How to use the Linux sed command to delete a range of lines

In a previous blog post I demonstrated how to use sed to insert text before or after a line in many files, and in this example I’ll show how to delete a range of lines using the sed command.

sed delete - How to delete a range of lines using sed

The problem I had today was that I just re-generated 99 HTML files for my Introduction to Unix/Linux tutorial using Latex2HTML, and it generates a bunch of “junk” in my HTML files that looks like this:

<a name="CHILD_LINKS"></a>
<ul>
<li><a name="tex2html5"
  href="node1.shtml">Contents</a>
<li><a name="tex2html6"
  href="node2.shtml">Introduction</a>
//
// this goes on for a long, long time ...
//
// it finally ends with this line:
<!--End of Table of Child-Links-->

Knowing that I want to delete this entire range of lines in all my generated HTML files, I used the following sed script to delete the range of lines, using this one sed command:

/\<!--Table of Child-Links-->/,/<!--End of Table of Child-Links-->/d

With this sed command, I start deleting lines in each file when I saw the first line pattern:

<!--Table of Child-Links-->

and then I stop deleting lines after seeing the ending line pattern:

<!--End of Table of Child-Links-->

Before running this sed command at all, I of course made a backup of all my files.

Then, after making that backup, I saved my sed command to a file named changes.sed:

/\<!--Table of Child-Links-->/,/<!--End of Table of Child-Links-->/d

As mentioned in my earlier article, I then tested my change by running the following command, and then visually verifying the output:

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

A Linux shell script to change all the files with sed

Once I was sure that my sed command had properly deleted the range of lines in that test case, I wrote a simple Linux shell script to run the sed command on every “node*.html” 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 my shell script ran, I checked some of my files, and verified that the sed script/command had successfully deleted that range of lines in all 99 files. The sed script worked fine, deleting the range of lines as desired, and life is good.

As quick followup note, if you ever need to just delete one line with sed, you can use this syntax:

/"header.shtml"/d

In this example I am looking for a line containing the string "header.shtml" (including the double quotes) and then deleting it, if and when it is found.

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.