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.
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/jpeek@jpeek.com/g $checksed home.html new.html
********** < = home.html > = sed output ********** 102c102 < <a href="mailto:jerry@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:jerry@ora.com">jerry@ora.com</a> -- > Page created by: <a href="mailto:jpeek@jpeek.com">jpeek@jpeek.com</a> ********** < = new.html > = sed output ********** 22c22 < <a href="mailto:jerry@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.
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 (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.)
-
, ,