home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

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.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:

  • It won't edit the sed script file (if you accidentally include sedscr on the command line).

  • It complains if you try to edit an empty file or something that isn't a file (like a directory).

  • If the sed script doesn't produce any output, runsed aborts instead of emptying your original file.

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.