Notification texts go here Contact Us Buy Now!

sed: insert a line in a certain position

Certainly! Here's the reformatted answer in HTML:

<p>So for the start, we have a file with the following lines, called datafile.txt</p>

<pre>1 some test lines here
but not all lines contain nubers
3 and here is the last one
</pre>

<p>and we have one bash variable $ADDED with the line content what want add</p>

<pre>ADDED="==This is the new line=="
</pre>

<h2>So, add line after the first line</h2>

<pre>ADDED="==This is the new line=="
< datafile.txt sed "1a \\
$ADDED
"
</pre>

<p>the result:</p>

<pre>1 some test lines here
==This is the new line==
but not all lines contain nubers
3 and here is the last line
</pre>

<h2>Add line after all lines what are starts with a number</h2>

<pre>< datafile.txt sed "/^[0-9]/a \\
$ADDED
"
</pre>

<p>the result:</p>

<pre>1 some test lines here
==This is the new line==
but not all lines contain nubers
3 and here is the last line
==This is the new line==
</pre>

<h2>Add line to the start, so insert before first line</h2>

<pre>< datafile.txt sed "1i \\
$ADDED
"
</pre>

<p>result</p>

<pre>==This is the new line==
1 some test lines here
but not all lines contain nubers
3 and here is the last line
</pre>

<h2>You can "substitute" the end of the line for adding a new one</h2>

<pre>< datafile.txt sed "/all/s/$/\\
$ADDED/"
</pre>

<p>the above example add line after the line what contains word "all" by substitution</p>

<pre>1 some test lines here
but not all lines contain nubers
==This is the new line==
3 and here is the last line
</pre>

<h2>You can even split line and add between</h2>

<pre>< datafile.txt sed "/all/s/\(.*lines \)\(.*\)/\1\\
$ADDED\\
\2/"
</pre>

<p>the above will search for the line what contains the word "all" and split it after the word "lines". The result:</p>

<pre>1 some test lines here
but not all lines 
==This is the new line==
contain nubers
3 and here is the last line
</pre>

<p>Last thing. It is impossible to parsing HTML with regural expressions, check the link in sputnik's comment.</p>

<p>BUT, that's not mean than it is impossible match some parts of HTML files. If you know what you want match (and not parse) - you can safely use regular expression for HTML too. Simply, many peoples here don't know the difference between parsing and matching.</p>

<p>So, if your html files has well known structure, e.g. you are sure than your html will the above structure all times, you can safely write:</p>

<pre><your_file.html sed "/^<tr><th>/a \\
<tr><td>new Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td>
"
</pre>

<p>and you will get</p>

<pre><table id="tfhover" class="tftable" border="1">
<tr><th>HEADER1</th><th>HEADER2</th><th>HEADER3</th><th>HEADER4</th></tr>
<tr><td>new Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td>
<tr><td>Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td></tr>
</table>
</pre>

<p>simply because we NOT PARSING the html code, we are only MATCHING some line patterns..</p>
</div>

Remember to remove the ``` tags when adding this to your blog post. If you have any further questions or need assistance with something else, feel free to ask.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.