LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2010, 01:55 AM   #1
password636
Member
 
Registered: Jun 2006
Location: Beijing
Posts: 60

Rep: Reputation: 1
how to replace a subtring with sed


Hi

If I have a line like AA=<value> in the file, and the <value> is not a constant. For example, it could be AA=BB or AA=CC. How can I replace the <value> with a specified string such as 'DD'? Thus, AA=BB can become AA=DD and AA=CC can also become DD. Don't want the whole line replaced, just the <value> part.

Can sed or awk work for this?
 
Old 09-19-2010, 02:13 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Yes you can do that with both sed and awk. If you know the length of the value; if that's static (always two characters) then you could use sed like this:
Code:
sed -i 's/=[A-Z][A-Z]/=DD/g' yourfile
The above will change all instances of '=' followed by two capital characters from the alphabet to =DD. Of course if the value is outside this scope; for example including numbers or being larger then two characters, you'll have to adapt the regex.

Sed will execute the changes inside de document without output on the screen. If you want to view the results on screen remove the -i. If you want the result in another file remove the -i and redirect output to another file.

Hope that helps.

Kind regards,

Eric
 
Old 09-19-2010, 02:16 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Sed can do this for sure. Is AA=value unique within the file? Is AA=value the only line in the file?

You would need something like
Code:
cat file | sed 's/^\(AA=\)\(.*\)/\1Insert Your New Value/' > file.tmp
mv file.tmp file
The above expression applies only to lines where the line starts with AA=. Change "Insert Your New Value" with whatever you want the new value of AA= to be.

Look up "regex sed" or "sed regex" for more information on how to form accurate expressions to suit your purpose.

Last edited by sag47; 09-19-2010 at 02:19 AM.
 
Old 09-19-2010, 02:25 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
@EricTRA: your solution lack of 'AA' pattern

@sag47:
- No need to 'cat' and pipe to 'sed'
- You should use "-i" (in-place) instead of redirect output to a file and move it back

@password636: you want the line begin with 'AA=' or including 'AA='? If I have the following input file:
Quote:
ijkAA=BB
AA=CC
AA=EF
AA=XYZ
What output do you want?

Last edited by quanta; 09-19-2010 at 02:35 AM.
 
1 members found this post helpful.
Old 09-19-2010, 02:28 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by quanta View Post
@EricTRA: your solution lack of 'AA' pattern

@sag47:
- No need to 'cat' and pipe to 'sed'
- You should use "-i" (in-place) instead of output to a file and move it back
Hello,

Quite right but I mentioned that in my post.
Quote:
The above will change all instances of '=' followed by two capital characters
I didn't state all istances of AA= followed by.....

I'm sure OP is able to 'fill in the blanks'.

Kind regards,

Eric
 
Old 09-19-2010, 02:46 AM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by quanta View Post
@sag47:
- No need to 'cat' and pipe to 'sed'
- You should use "-i" (in-place) instead of redirect output to a file and move it back
I prefer backups for my backups. It's not like my command wouldn't work (there's a 1000 ways to build a house). Whatever the case may be, as Eric puts it, the OP can easily modify all the information we've given him/her to their liking. Thanks for the tips.


Last edited by sag47; 09-19-2010 at 04:56 AM.
 
Old 09-19-2010, 04:43 AM   #7
password636
Member
 
Registered: Jun 2006
Location: Beijing
Posts: 60

Original Poster
Rep: Reputation: 1
Hi guys really thank you all!
@quanta:
beginning with 'AA=' is OK.
 
Old 09-19-2010, 04:57 AM   #8
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
# cat file
AA=BB dog cat AA=CC dog cat
mouse rat AA=xxxxxAA=yyyyy

# ruby -e 's=readlines.join.split("AA=").each{|x| x.sub!(/^.[^\s]*/,"AA=DD")};print s.join' < file
AA=DD dog cat AA=DD dog cat
mouse rat AA=DDAA=DD

sed is greedy.

Code:
# sed 's/^\(AA=\)\(.*\)/\1DD/' file
AA=DD
mouse rat AA=xxxxxAA=yyyyy
Wrong output.
 
1 members found this post helpful.
Old 09-19-2010, 05:14 AM   #9
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Shouldn't matter if it is a settings/config file where usually it is one setting per line in a setting=value format. I designed the regex to be greedy for simplicity's sake. It can be refined to be less or more greedy you just have to modify the regex.

Using ruby is an interesting solution for it though.

Last edited by sag47; 09-19-2010 at 05:18 AM.
 
  


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
how to replace with variable using sed? babag Programming 4 09-17-2008 03:28 PM
sed search replace tomerbd1 Linux - General 9 04-10-2008 04:31 AM
How do I replace ' with sed.... @ngelot Linux - Newbie 2 11-02-2007 07:04 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 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