sed example - 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.)

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.

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\
<br/>

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.

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`
do
  echo "working on $i ..."
  sed -f changes.sed < $i > ${i}.tmp
  mv ${i}.tmp $i
done

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

sh go.sh

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

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
/<\/body>/i\
</div>

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.

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.

What is difference between

What is difference between grep and sed

sed and grep

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.

Post new comment

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