• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Techgoeasy

Techgoeasy

Learn Oracle, PHP, HTML,CSS,Perl,UNIX shell scripts

  • Home
  • Oracle
    • Oracle database
    • Oracle Ebusiness Suite
    • Oracle weblogic
    • Oracle Performance Tuning
    • Oracle Hyperion
    • Oracle Cloud Tutorials
  • SQL
  • interview questions
  • Linux
  • PHP and HTML
  • Downloads
  • General
Home » Unix command and scripts » How to insert line after match using sed

How to insert line after match using sed

February 15, 2023 by techgoeasy Leave a Comment

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

Table of Contents

  • sed add line after match
  • sed multiple line after match
  • sed add line before match
  • sed insert after line number

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

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

Filed Under: Unix command and scripts

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar



Subscribe to our mailing list

Enter your email address to subscribe to this blog and receive notifications of new posts by email

Recent Posts

  • How to find the bind variable of the sql id
  • How to list parameter set at session level in Oracle
  • How to generate tkprof in EBS in 19c
  • Oracle tkprof utility
  • What is Skew in Database?

Copyright © 2023 : TechGoEasy

  • Hire me
  • Privacy Policy
  • Contact Us
  • New? Start Here
  • About Us