LinuxQuestions.org
Review your favorite Linux distribution.
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 04-28-2008, 03:33 AM   #1
kambrish
LQ Newbie
 
Registered: Jun 2007
Location: India
Posts: 12

Rep: Reputation: 0
search and replace with multi line string


Hi,
I am using the sed command for search and substitute but now i have a problem here.
Normally, i do:

sed s/ForThis/NewThis/g filename.txt

This will search
ForThis and replace it with NewThis in file filename.txt

Now, I want to do the same thing but substitution should happen on 2 lines as:
ForThis and replace it with NewThis1 and NewThis2 in file filename.txt as below:
NewThis1
NewThis2

If i want on same line, no problems. But i have to get these on 2 separate lines one after the other. Can someone help. I tried with \n but its not taking
sed s!"ForThis"!"NewThis1\nNewThis2"! filename.txt

Regards Ambrish
 
Old 04-28-2008, 03:37 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Here the \n symbol is interpreted as a litteral n. Try to embed it in double quotes to prevent excaping from the shell
Code:
sed s/ForThis/NewThis1"\n"NewThis2/g filename.txt
or enclose the sed command in quotes
Code:
sed 's/ForThis/NewThis1\nNewThis2/g' filename.txt

Last edited by colucix; 04-28-2008 at 03:38 AM.
 
Old 04-28-2008, 04:04 AM   #3
kambrish
LQ Newbie
 
Registered: Jun 2007
Location: India
Posts: 12

Original Poster
Rep: Reputation: 0
Not working

Got error as:
sed s/ForThis/NewThis1"\n"NewThis2/g filename.txt
bash: /ForThis/NewThis1"\n"NewThis2/: event not found

Tried like this:
sed 's/ForThis/NewThis1\nNewThis2/g' filename.txt
It printed on same line as below:
NewThis1nNewThis2

Then, tried as below:
sed 's/ForThis/NewThis1\\nNewThis2/g' filename.txt
It printed on same line as below:
NewThis1\nNewThis2

Again tried as below:
sed 's/ForThis/NewThis1'\n'NewThis2/g' filename.txt
Result is
NewThis1nNewThis2


Any other option?
 
Old 04-28-2008, 05:12 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Maybe your version of sed does not support newline escapes in the replacement string. Which version is it and on which system?

Anyway, you can try something like this
Code:
sed '/ForThis/{s/ForThis/NewThis1/;G;s/$/NewThis2/}'
this will substitute the string ForThis with NewThis1, then add a new line (G command) and finally substitute the new line with NewThis2. Just three steps instead of one.
 
Old 04-28-2008, 06:15 AM   #5
kambrish
LQ Newbie
 
Registered: Jun 2007
Location: India
Posts: 12

Original Poster
Rep: Reputation: 0
nopes
Per your suggesstion, this is the exact input i gave (this is my project requirement):

sh -c '(su - rgc3 -c " ( cd ini ; sed /ResourceName0=/{s/ResourceName0=/ResourceName0=RGC_FIP/;G;s/ResourceName1=OAM_FIP/} "gnetd.ini" >gnetd_dummy ) ")' > ./.stdout_565 2> ./.stderr_565

And the error i got:
more ./.stderr_565
Too many {'sksh: G: not found
ksh: s/ResourceName1=OAM_FIP/}: not found
 
Old 04-28-2008, 07:02 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Ok. Try to escape the semi-colons, otherwise the shell will interpret them as command separators. Also, you forgot the search pattern /$/ in the second substitution. See the parts in red in the corrected command line:
Code:
sh -c '(su - rgc3 -c " ( cd ini ; sed /ResourceName0=/{s/ResourceName0=/ResourceName0=RGC_FIP/\;G\;s/$/ResourceName1=OAM_FIP/} "gnetd.ini" >gnetd_dummy ) ")' > ./.stdout_565 2> ./.stderr_565
 
  


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
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Need help with VI search/replace string thanos79 Programming 6 04-10-2008 03:43 AM
Need help with VI search/replace string thanos79 Ubuntu 1 04-10-2008 01:00 AM
Perl Multi-Line Search & Replace... Can I?... donv2 Linux - Newbie 2 01-30-2007 10:59 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


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