• 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

January 1, 2019 by techgoeasy Leave a Comment

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

Let’s take an example to understand it

We have a html file like below

$ cat test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
This is test page
</body>
</html>

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

we can achieve it using sed with append

sed -e ‘/<body>/a  Welcome everybody’ test.html
<html>
<head>
<title>This is test</title>
</head>
<body>
Welcome everybody
This is test page
</body>
</html>

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. File is not changed. If you want to change the file also

we can do with -i option

#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 pattern the file

sed -i -e ‘/<body>/a  Welcome everybody’ *.html

Hope you like this short article on sed. Please do let me know your feedback

Related Articles

Awesome sed command in Linux/Unix with examples

21 awk command examples in Unix/Linux

Useful Unix/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 start/stop Node manager and Managed server in Weblogic
  • Weblogic Administration Console
  • How to enable SSL in Oracle Weblogic
  • How to stop-start WebLogic AdministrationServer
  • ORA-28007: the password cannot be reused

Copyright © 2022 : TechGoEasy

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