Home » Unix command and scripts » How to insert line after match using sed

How to insert line after match using sed

I was working on a certain project last week where I need to insert a line after the match in 100 of files in Linux or Unix operating system. I found that inserting a line after a match using sed is easy. It was pretty successful for me, so I thought I will share this on my blog.

Let’s take an example to understand it

We have an html file like below

$ cat test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
This is test page
</body>
</html>
sed add line after match

sed add line after match

I need to enter certain text Welcome everybody after like <body> in the file

we can achieve it using sed with append

sed -e '/<body>/aWelcome everybody' test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
Welcome everybody
This is test page
</body>
</html>

So far the above command just changes the output. The file is not changed. If you want to change the file also

we can do this -i option

#sed -i -e '/<body>/aWelcome everybody' test.html
#cat test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
Welcome everybody
This is test page
</body>
</html>

If you want to preserve leading space at the start of the new line

#sed -i -e '/<body>/a / /Welcome everybody' test.html 
#cat test.html 
<html> 
<head> 
<title>This is test</title> 
</head> 
<body> 
  Welcome everybody 
This is test page 
</body> 
</html>
sed insert line after match

You can make these changes in multiple files using *  or giving a pattern to the file

sed -i -e '/<body>/aWelcome everybody' *.html

sed multiple line after match

You can do using \n operator as given below

sed -e '/<body>/aWelcome everybody\nWelcome everybody' test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
Welcome everybody
Welcome everybody
This is test page
</body>
</html>

sed add line before match

if I need to enter certain text before  like <body> in the file

#sed -e '/<body>/i Welcome everybody' test.html
<html>
<head>
<title>This is test</title>
</head>
Welcome everybody
<body>
This is test page
</body>
</html>

So far the above command just changes the output. The file is not changed. If you want to change the file also

See also  Find command

we can do this -i option

#sed -i -e '/<body>/i  Welcome everybody' test.html
#cat test.html
<html>
<head>
<title>This is test</title>
</head>
Welcome everybody
<body>
This is test page
</body>
</html>

sed insert after line number

This is easier, you just need to specify the line number. If you have insert after line ,below command can be used

#cat test.html
<html>
<head>
<title>This is test</title>
<body>
This is test page
</body>
</html>
#sed '2a Welcome everybody' test.html
<html>
<head>
Welcome everybody
<title>This is test</title>
<body>
This is test page
</body>
</html>

If you want to do before the line number

# sed '2i Welcome everybody' test.html
<html>
Welcome everybody
<head>
<title>This is test</title>
<body>
This is test page
</body>
</html>

Hope you like this short article on sed add line after match, sed add line before match, sed insert after line number. Please do let me know your feedback

Related Articles

sed command in Linux/Unix with examples
awk command examples in Unix/Linux
Linux command for Oracle DBA
Writing loop statement in Unix shell scripting with examples

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top