Sed command insert lines before or after pattern

Share this post to your friends !

Sed command insert lines before or after pattern
5 (100%) 1 vote[s]

WriteBash - How to insert lines before or after pattern? In this article, I will show you how to use sed command to insert lines before or after the pattern.

Use sed command to insert

In the work of writing scripts to install services for the system. Inserting lines into configuration files (eg IPtables, Nginx ..) is a regular thing.

Sed command, when you use it to replace or insert text lines by default, the result changes only to the screen, not to the file we are editing.

To write the result of sed command to the file, use the -i option.

$ sed -i [insert-your-lines] [filename]
-i[SUFFIX], --in-place[=SUFFIX]
             edit files in place (makes backup if SUFFIX supplied)

Insert lines before match pattern

sed-command-insert-lines-before-or-after-pattern-01 Sed command insert lines before or after pattern
Use sed commadn to insert lines before pattern.

To insert a line before the match pattern, use the sed command as follows.

$ sed '/pattern/i new-line' [filename]

For example, we have the text file sed-demo.txt with the content below.

linuxmint writebash.com # cat sed-demo.txt 
line number 01
line number 03
line number 04
line number 06
#line comment

We will insert the line number 02 line right before the line number 03 line.

linuxmint writebash.com # sed '/line number 03/i line number 02' sed-demo.txt 
line number 01
line number 02
line number 03
line number 04
line number 06
#line comment

With the above command, because we have many lines starting with line number so the pattern we have to write exactly the line number 03.

There is a different line in the file that I intentionally created, starting with the # character.

Now we will insert a new line immediately before this # line.

linuxmint writebash.com # sed '/^#/i line number 02' sed-demo.txt 
line number 01
line number 03
line number 04
line number 06
line number 02
#line comment

You can now see the pattern between two different commands. In the first insert command is line number 03, and the second insert command is ^#.

Insert line after match pattern

sed-command-insert-lines-before-or-after-pattern-02 Sed command insert lines before or after pattern
Use sed command to insert lines after pattern.

Similar to inserting a line before a pattern, insert the line after the pattern as follows.

$ sed '/pattern/a new-line' [filename]

Also with the above file, we insert line number 05 after the line number 04.

linuxmint writebash.com # sed '/line number 04/a line number 05' sed-demo.txt 
line number 01
line number 03
line number 04
line number 05
line number 06
#line comment

And insert line number 05 after the line #line comment.

linuxmint writebash.com # sed '/^#/a line number 05' sed-demo.txt 
line number 01
line number 03
line number 04
line number 06
#line comment
line number 05

Insert multiple lines

Above are examples of inserting a single line, now if you want to insert multiple lines. You use \n between the new text lines that need to be inserted.

The command will look like this.

$ sed '/pattern/a new-line-01\nnew-line-o2' [filename]

Conclusion

Through this article, you learned how to use sed command to insert text lines between configuration files that match the pattern. Inserting text lines before or after depends on patterns, not always the same.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.