1833271 Members
3240 Online
110051 Solutions
New Discussion

Re: SED questions

 
SOLVED
Go to solution
Ragni Singh
Super Advisor

SED questions

Hi all,

In UNIX, using "sed", if I want to replace string 'cat' with string 'dog', it is -
sed -e 's/cat/dog'

But if I want to replace 'cat' with 'cat,newline char,dog,newline char' how do I do it ???

Any help is greatly appreciated and points will be given.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: SED questions

Hi:

You can do this:

# sed -e 's/cat/dog\^J/'

...where a backslash follows "dog" and you hold the CONTROL key plus the letter "v" down and hit the return or enter key.

Regards!

...JRF...
S.K. Chan
Honored Contributor

Re: SED questions

Try this ..let say the file is called "test".

$ sed 's/cat/cat> dog> /g' test > newtest

That should replace all instances of "cat" with "cat dog "
S.K. Chan
Honored Contributor

Re: SED questions

Sorry .. formatting is all wrong ..

$ sed 's/cat/cat\dog\/g' test > newtest
James R. Ferguson
Acclaimed Contributor

Re: SED questions

Hi (again) Sanjay:

If you mean, literally. "replace 'cat' with 'cat,newline char,dog,newline char'", then:

# sed -e 's/cat/cat\^Jdog\^J/g'

...composing the ^J sequence (newline character) as I noted above.

Drop the 'g' flag if you want to do this only once per line.

Regards!

...JRF...
Rory R Hammond
Trusted Contributor

Re: SED questions

Sanjay,

I tried all of the above and could not get them to work from my hpux command line.

the closet I could come was with the following:

sed -e "s/cat/`echo "\rdog\r"`/g" test |tr "\r" "\n" >newtest

I hope you did better. I have a url that has all kinds of handy sed one liners:
http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html

none seemed to solve your problem.

Rory
There are a 100 ways to do things and 97 of them are right