LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Inserting Multiple Lines (with newline) using sed or awk (https://www.linuxquestions.org/questions/programming-9/inserting-multiple-lines-with-newline-using-sed-or-awk-710100/)

hal8000b 03-08-2009 05:02 PM

Inserting Multiple Lines (with newline) using sed or awk
 
So far, I've only done basic substitution with sed, so dont know if this is possible.

I need to replace part of a line in a file with multiple lines, however need to separate each line with a newline is this possible?

Example, need to remove line 2
</head>
<h1>My Blog</h1>

and insert line similar to below:
--snip
</head>
<body background="gifs/blue.gif">
<table class="table-head">
<tr>
<td><img src="jpgs/image_a.jpg" border="0"></td>
<td valign="middle" align="center" style="font-size: 40px; color:red">My Blog</td>
<td><img src="jpgs/image_b.jpg" align="right" border="0"></td>
</tr>

If the multiple lines were part of a text file, would inserting a text file automatically include newline characters?


Thanks in advance to anyone who can help?

Telemachos 03-08-2009 05:21 PM

Put the lines you want to add into another file, and then feed that into your sed command using the r filename syntax.

Here's an example:
Code:

telemachus ~/practice $ cat practice
foo
bar
baz
telemachus ~/practice $ cat file
cats
love
dogs
telemachus ~/practice $ sed '/foo/ r file' < practice
foo
cats
love
dogs
bar
baz

See here for more: http://www.grymoire.com/Unix/Sed.html#uh-37


All times are GMT -5. The time now is 11:38 AM.