Unix Power ToolsUnix Power ToolsSearch this book

34.4. Testing and Using a sed Script: checksed, runsed

All but the simplest sed scripts are often invoked from a "shell wrapper," a shell script (Section 35.2) that invokes sed and also contains the editing commands that sed executes. A shell wrapper is an easy way to turn what could be a complex command line into a single-word command. The fact that sed is being used might be transparent to users of the command.

Two shell scripts that you should immediately arm yourself with are described here. Both use a shell for loop (Section 35.21) to apply the same edits to any number of files. But the first just shows the changes, so you can make sure that your edits were made correctly. The second writes the edits back into the original file, making them permanent.

34.4.1. checksed

Figure Go to http://examples.oreilly.com/upt3 for more information on: checksed

The shell script checksed automates the process of checking the edits that sed makes. It expects to find the script file, sedscr, in the current directory and applies these instructions to the input files named on the command line. The output is shown by a pager program; the default pager is more .

#! /bin/sh
script=sedscr

for file
do
        echo "********** < = $file    > = sed output **********"
        sed -f $script "$file" | diff "$file" -
done | ${PAGER-more}

For example:

$ cat sedscr
s/jpeek@ora\.com/jpeek@jpeek.com/g
$ checksed home.html new.html
********** < = home.html    > = sed output **********
102c102
< <a href="mailto:jpeek@ora.com">Email it</a> or use this form:
--
> <a href="mailto:jpeek@jpeek.com">Email it</a> or use this form:
124c124
< Page created by: <a href="mailto:jpeek@ora.com">jpeek@ora.com</a>>
--
> Page created by: <a href="mailto:jpeek@jpeek.com">jpeek@jpeek.com</a>
********** < = new.html    > = sed output **********
22c22
< <a href="mailto:jpeek@ora.com">Send comments</a> to me!
---
> <a href="mailto:jpeek@jpeek.com">Send comments</a> to me!

If you find that your script did not produce the results you expected, perfect the editing script and run checksed again.

34.4.2. runsed

Figure Go to http://examples.oreilly.com/upt3 for more information on: runsed

The shell script runsed was developed to make changes to a file permanently. It applies your sedscr to an input file, creates a temporary file, then copies that file over the original. runsed has several safety checks:

runsed only modifies a file if your sedscr made edits. So, the file's timestamp (Section 8.2) won't change if the file's contents weren't changed.

Like checksed, runsed expects to find a sed script named sedscr in the directory where you want to make the edits. Supply the name or names of the files to edit on the command line. Of course, shell metacharacters (Section 33.2) can be used to specify a set of files:

$ runsed *.html
runsed: editing home.html:
runsed: done with home.html
runsed: editing new.html:
runsed: done with new.html
runsed: all done

runsed does not protect you from imperfect editing scripts. You should use checksed first to verify your changes before actually making them permanent with runsed. (You could also modify runsed to keep backup copies of the original versions.)

--DD, JP, and TOR



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.