How to replace string with some text (with new lines) sed or something else ?

How to replace string with some text (with new lines) sed or something else ?

Post by Andrey Koubyche » Thu, 27 Feb 2003 01:24:22



Hello everyone

I try to replace text string in file on few lines of text from shell var.
This new text contains new lines First my idea was to use sed. I've tryed
the following :

sed -e "s/old/\nnew/" file

wrong.
after searching on google:

sed -e "s/old/\
new/" file

Last variant work from command line but somehow does not work from script
shell (bash)

Question , what do I do wrong ?  Probably some tricky quoting.
It is not necessary to use sed, something else is ok too. just not something
* :)

--
Best regards,
Andrey Koubychev

 
 
 

How to replace string with some text (with new lines) sed or something else ?

Post by kim » Thu, 27 Feb 2003 02:36:53



> Hello everyone

> I try to replace text string in file on few lines of text from shell var.
> This new text contains new lines First my idea was to use sed. I've tryed
> the following :

> sed -e "s/old/\nnew/" file

> wrong.
> after searching on google:

> sed -e "s/old/\
> new/" file

> Last variant work from command line but somehow does not work from script
> shell (bash)

> Question , what do I do wrong ?  Probably some tricky quoting.
> It is not necessary to use sed, something else is ok too. just not something
> * :)

> --
> Best regards,
> Andrey Koubychev

if you want to use sed, this issue is handled in the faq, under item 4.6:
http://www.veryComputer.com/

 
 
 

How to replace string with some text (with new lines) sed or something else ?

Post by Andrey Koubyche » Thu, 27 Feb 2003 21:52:46


Answering to myself :)
The most comfortable solution is to use ed:

/bin/ed -  file > /dev/null << \EOF
/pattern/
a
line1
line2
line3
.
w
q
EOF

--
Best regards,
Andrey Koubychev