LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-28-2008, 02: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, 02:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 02:38 AM.
 
Old 04-28-2008, 03: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, 04:12 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 05: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, 06:02 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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

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

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