Table of Contents
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

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 commentWe 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 commentWith 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 commentYou 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

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 commentAnd 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 05Insert 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).