Sed command insert lines before or after pattern

by Daniel Pham
Published: Updated:

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

use sed commadn to insert lines before 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.

Recommended Reading: How to write a bash script

$ 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.

$ 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.

$ 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

use sed command to insert lines 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.

Recommended Reading: Format of a bash script file

$ 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.

$ 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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.