1821539 Members
2386 Online
109633 Solutions
New Discussion юеВ

add a line with sed

 
Rene de Groot
Occasional Advisor

add a line with sed

How can i add a new line (with
commands) in the middle of a file after
a specific word using sed.
example : this is a file
whitch will
be added

I would like to add a line after 'will'
whitch include the line 'sometimes'
so it would become:
this is a file
whitch will
sometimes
be added
5 REPLIES 5
Kenneth_19
Trusted Contributor

Re: add a line with sed

First, prepare the command file with the following content (2 lines), name the file append.cmd (or whatever you like):

~~~~~~~~~~~~~~~~~~start~~~~~~~~~~~~~~~~~~
/will/ asometimes
~~~~~~~~~~~~~~~~~~~end~~~~~~~~~~~~~~~~~~~

Then, run the command:

sed -f append.cmd target.file > output.file

The output.file will contains what you want.

Regards,
Kenneth
Always take care of your dearest before it is too late
Kenneth_19
Trusted Contributor

Re: add a line with sed

The command file is not displayed correctly, please get it from the attachment.

Regards,
Kenneth
Always take care of your dearest before it is too late
Rene de Groot
Occasional Advisor

Re: add a line with sed

thanks Kenneth your solution works
but is it also possible to add more words at once and save it in the same file

regards Rene
James Beamish-White
Trusted Contributor

Re: add a line with sed

Hiya,

See if the following thread helps:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x611f4b3ef09fd611abdb0090277a778c,00.html

Basically, you can just use >>'s to append to the file until it's constructed appropriately.

Cheers!
James

If it's hard, you're doing it wrong
GARDENOFEDEN> create light
Aashish Raj
Valued Contributor

Re: add a line with sed

sed -e 's/will/willsometimes/
s/old word/new word to be replaced/' file >newfile
mv newfile file

"\" is followed by newline

HTH
AR