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
$ 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
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
Now we will insert a new line immediately before this
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
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
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
line number 05 after the line
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]
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.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.