sed examples: how to insert text before and after existing lines

If you ever need to use the Unix/Linux sed command to insert text before or after a line of text in an existing file, here's how I just ran several sed commands to update my old Function Point Analysis tutorial to have a format that doesn't look like it was created in the 1990s.

This tutorial consists of over 40 files, and I had eight changes I wanted to make each file. So I had two choices: modify each file by hand over the next six hours, or run a series of sed commands and be done in 30 minutes. (I chose the sed commands.)

Back to top

sed example - Step 1

Because you can easily screw up a lot of files with one sed command, Step 1 is to make a backup of whatever files you're thinking about modifying with sed. Then move that backup to a different directory.

Skip this step at your own risk. You've been warned.

Back to top

sed example - insert a line after another line

To demonstrate a simple example, here's how I used sed to insert an HTML break tag after a line that contained the text string "header.shtml". After looking at a few files I knew this string (a) was in every file and (b) was unique in those files, so I wrote this sed command:

/header.shtml /a\

I saved that sed command in a file named changes.sed.

Next, to make sure I remembered the sed syntax for inserting new text after an existing line, I ran a test sed command from the Linux command line like this:

sed -f changes.sed < node2.shtml | more

This command didn't actually change the file node2.shtml, it just reads the file, and then makes the changes as it writes the file contents to standard output. I piped that sed command output into the Linux "more" command so I could make sure my change worked as expected.

Back to top

A Linux shell script to change all the files with sed

Satisfied that my change looked correct on this one file, I wrote a simple Linux shell script to run my sed command on every file in the current directory, like this:

# change all the "node*.html" files in the current directory

for i in `ls node*html`
  echo "working on $i ..."
  sed -f changes.sed < $i > ${i}.tmp
  mv ${i}.tmp $i

I saved this shell script to a file named, and then ran it like this:


After it ran, I checked some of my files, and this simple "sed insert after" example worked just fine.

Back to top

A sed script to insert text before and after a line

That sed example demonstrated how to insert text after a given line in a text file. Next, here's a sed script I used to insert two HTML "div" tags, the first one after the opening body tag, and the second one before the closing body tag.

# insert a div tag *after* the opening body tag
/<body /a\
<div id="wrap_body">

# insert the closing div tag *before* the closing body tag

Again, I tested this sed script like this:

sed -f changes.sed < node2.shtml | more

and once I was sure everything looked right, I again ran my Linux shell script to modify all the HTML files in the current directory.

Back to top

sed example - inserting text before and after lines

I hope this short example of how to use the sed command to insert text before and after lines in many text files has been helpful. If you have any related sed command examples you'd like to share, just use the Comments section below. Or, if you'd like to write a sed tutorial or two, consider writing your own tutorials on the devdaily website.

Back to top

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

The grep command is basically a text search command, meaning you can use it to find text strings in files. The sed command stands for "streamline editor", and can actually be used to edit files without the use of a traditional editor (vi, emacs). Just search the site for "grep" or "sed" and you should find some examples. Cheers.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.