LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-11-2013, 07:24 PM   #1
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Rep: Reputation: 28
sed help. Search and replace multiple strings with one command.


I'm having an issue trying to search and replace multiple strings in a file. Here is an example of the sed command I'm trying to run.

Code:
sed -e 's/string1/string2/g;s/string2/string3/g' /file > /new_file
Example input file:
Code:
string1
string2
Example output file:
Code:
string3
string3
Desired output file:
Code:
string2
string3
So it looks like sed does one pass of the file with the first expression and then does a pass with the second expression. This results in changes from the first expression effecting the input to the second expression. Is there any way I can apply both of the expressions to the original file to get my desired output?
 
Old 02-11-2013, 07:34 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,590

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
The semicolon tells sed to do exactly what it is doing, search for this and do what I tell you to, then once that is complete here is the next string. I'm not going to be the one but if you simply switch the order of your strings you would avoid your second search picking up the string1's that were changed to string2.

So you do

Code:
sed -e 's/string2/string3/g;s/string1/string2/g' /file > /new_file
And yes there are other ways to do this and we could go into deeper talk about alternatives but this is easy. If you want more info just ask.
 
Old 02-11-2013, 07:40 PM   #3
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by Kustom42 View Post
The semicolon tells sed to do exactly what it is doing, search for this and do what I tell you to, then once that is complete here is the next string. I'm not going to be the one but if you simply switch the order of your strings you would avoid your second search picking up the string1's that were changed to string2.

So you do

Code:
sed -e 's/string2/string3/g;s/string1/string2/g' /file > /new_file
And yes there are other ways to do this and we could go into deeper talk about alternatives but this is easy. If you want more info just ask.
Thanks for the comment unfortunately my situation is a little more complicated than the example. In my case the strings are variables and therefore I can not simply switch the order to fix the problem.
 
Old 02-12-2013, 12:38 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

The tool called trs (an analog of tr for strings) seem to do what you want. On Debian this tool comes with the konwert package.

For example:
Code:
$ echo -e 'str1\nstr2'  | trs 'str1 str2 str2 str3'
str2
str3
 
Old 02-13-2013, 12:45 PM   #5
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Original Poster
Rep: Reputation: 28
Thanks firstfire. That tool looks like just what I'm looking for. Unfortunately this script is for an embedded system which does not have the trs tool.

I have since created a solution which involves looping through each line of the input file and making the appropriate string substitution based on which original string is grepped on the line. Since each line can only have one original string this solution will work for me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed command search and replace zulkifal Linux - Newbie 8 11-26-2012 11:56 AM
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 11:38 AM
[SOLVED] how we can find and replace multiple file using sed command jayakumar01 Linux - Server 1 04-05-2012 08:53 AM
Search and replace strings in text files in C Completely Clueless Programming 5 09-15-2009 07:51 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 03:32 PM


All times are GMT -5. The time now is 11:51 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration