UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.2 Invoking sed Chapter 34
The sed Stream Editor
Next: 34.4 sed Addressing Basics
 

34.3 Testing and Using a sed Script: checksed, runsed

All but the simplest sed scripts are often invoked from a "shell wrapper," a shell script (44.1) 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 (44.16) 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.3.1 checksed

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/jerry@ora\.com/[email protected]/g
$ checksed home.html new.html
********** < = home.html    > = sed output **********
102c102
< <a href="mailto:[email protected]">Email it</A> or use this form:
--
> <a href="mailto:[email protected]">Email it</A> or use this form:
124c124
< Page created by: <a href="mailto:[email protected]">[email protected]</a>
--
> Page created by: <a href="mailto:[email protected]">[email protected]</a>
********** < = new.html    > = sed output **********
22c22
< <a href="mailto:[email protected]">Send comments</A> to me!
---
> <a href="mailto:[email protected]">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.3.2 runsed

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 (16.5) 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. (Article 4.3 describes a way to keep many sed scripts.) Supply the name or names of the files to edit on the command line. Shell metacharacters (15.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, TOR


Previous: 34.2 Invoking sed UNIX Power ToolsNext: 34.4 sed Addressing Basics
34.2 Invoking sed Book Index34.4 sed Addressing Basics

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System