LinuxQuestions.org
Visit Jeremy's Blog.
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 01-31-2007, 06: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, 06: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, 07:30 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09:00 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

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



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

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

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