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 01-31-2007, 07:10 AM   #1
rohit2983
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Rep: Reputation: 0
substituting multiline string in sed


Hi Experts

I am facing a problem while using sed to replace a multiline string

I have sed command stored in a file say command

command file has the following content

s/"Owner"/"Owner_T"/g
s/"Contract
Number"/"Contractnumtest"/g

I want to replace
"Contract
Number" string with "Contractnumtest"
the file with name say "file" which i have to edit is something like this

"Owner"
"Contract"
"Contract
Number"

when i try to use sed to edit by doing this

sed -f commands $file > file_outptut1.txt

I am getting errors like
sed: command garbled: s/"Contract

I am using Solaris as OS.

Is it possible to replace the multiline string using sed or sed commands have to be end in one line???

any solution/suggestion/pointers to solve this problem are welcome

thanks in advance for helping
 
Old 01-31-2007, 07:59 AM   #2
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
You need to consult the sed&awk bible! Here:
http://www.unix.org.ua/orelly/unix/sedawk/ch06_01.htm

Basically you should be able to use what's called the "hold space" to recognise and remember stuff from multiple lines.

HTH
 
Old 01-31-2007, 08:30 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Your command file should look something like this:

s/"Owner"/"Owner_T"/
/"Contract$/{
N
s/"Contract\nNumber"/"Contractnumtest"/
}


Nick_Battle gave good advice: All the info needed is in the Sed&Awk book.
 
Old 01-31-2007, 10:00 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

I give you no extra magic, however, a rule that works for me is to remember that sed deals with single-lines unless you do some extra work.

I could not get the URL that Nick posted to work ( page deleted ). If you can read Russian, perhaps some of the links at that top page might help.

There are some other places to look. The web page http://sed.sourceforge.net/ has a number of links that may help.

It is difficult to imagine a better reference than O'Reilly's sed and awk. I use the 2nd edition (page 107 ff for your circumstance); you might be able to pick up a used version someplace of either edition 1 or 2. I think sed is very useful in many situations, however, perl has probably taken over much of the new work for which sed had been used.

Best wishes ... cheers, makyo
 
  


Reply

Tags
sed


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
Remove string in sed twantrd Programming 7 09-13-2006 03:28 PM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
insert string with sed greg108 Programming 7 02-18-2005 02:11 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM
need help substituting with sed parker Programming 2 08-08-2003 11:03 AM


All times are GMT -5. The time now is 11:37 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration