LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-27-2015, 12:45 PM   #1
RM2015
LQ Newbie
 
Registered: May 2015
Posts: 3

Rep: Reputation: Disabled
Smile Search for a character in specific word in file and replace it in the word


Hi all ,
I have a requirement where I have a file. Contents of the file are :
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678-vg="https://www.bhy.com"
IC-gy_567:78="https://www.gyt.com"
#comments
The variable names can not have characters like - , : so
in this file I have to find words starting with IC and replace characters like - ,:
I want to change only the variable name , not the whole line.
I have used SED command

sed -i '/^IC/s/[^0-9 a-z A-Z _]*//g' file

when I am using this command , it is replacing the whole line
output becomes :

#comments
VAR="abg"
RES=123
#comments
IC6790ABGhttpswwwabccom
IC5678vghttpswwwbhycom
ICgy56778httpswwwgytcom
#comments


But I want the output like this :

#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678vg="https://www.bhy.com"
ICgy_56778="https://www.gyt.com"
#comments

How can I get the desired output , thanks for your help in advance .
 
Old 05-27-2015, 05:03 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Similar solution here:
http://www.linuxquestions.org/questi...7/#post5366219
You just need to capture the match with () and replace it.
 
Old 05-28-2015, 01:54 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,137

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
That might be a bit difficult in sed - gsub in awk would be cleaner.
 
Old 05-28-2015, 05:23 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
sed 's/^IC\(?*\)-\(?*\)/IC\1\2/g' /tmp/new
Where /tmp/new is your original file gives:
Code:
keithhedger@LFSCerebro:/tmp-> cat /tmp/new
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678-vg="https://www.bhy.com"
IC-gy_567:78="https://www.gyt.com"
#comments
keithhedger@LFSCerebro:/tmp-> sed 's/^IC\(?*\)-\(?*\)/IC\1\2/g' /tmp/new
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678-vg="https://www.bhy.com"
ICgy_567:78="https://www.gyt.com"
#comments
Either use redirect to store the output to a file or use the '-i' switch to sed to change the file in place.
 
Old 05-28-2015, 05:46 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,137

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
What about the semi-colons ?.
How do you know what order they will be in ?.

Seems comma is also a possibility - same question about order for the testing.
 
Old 05-28-2015, 06:04 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
syg00 I've given you the answer, you need to read the sed manual and do a bit of research extra characters to be deleted are easy to add if you do a bit of the work yourself, I'm not doing it all for you, this is moderately easy stuff, if you are going to learn Linux sooner or later you will have to learn regex/sed/awk, last hint use '[]' character sets.
 
Old 05-28-2015, 07:07 AM   #7
RM2015
LQ Newbie
 
Registered: May 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Keith , I will try your solution and let you know if it works.
 
Old 05-28-2015, 01:43 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I feel your a little heavy handed on the comments there Keith. Your solution provided did not mention that it was a part solution to be added to and syg00 is not the OP and as a veteran user is
quite familiar with sed. I would reiterate what has been said that your solution will not work so well for the full colon:
Code:
sed 's/^IC\(?*\):\(?*\)/IC\1\2/g' file
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678-vg="https://www.bhy.com"
IC-gy_567:78="https://www.gyt.com"
#comments
This output shows no changes whatsoever to the example data
 
Old 05-28-2015, 02:01 PM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Oh dear sorry wasn't I touchy feally enough for you didums!
 
Old 06-02-2015, 07:38 AM   #10
RM2015
LQ Newbie
 
Registered: May 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi ,

I used sed 's/^IC\(.*\)-\(.*\)=/IC\1\2=/g;s/^IC\(.*\):\(.*\)=/IC\1\2=/g' /tmp/new
as suggested by Keith and it worked , thank you so much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
How to build a script that will search for a specific word in a log file kimbo8 Programming 1 03-27-2012 05:17 AM
Replace a specific word every three occurences udiubu Programming 5 12-07-2011 10:58 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration