<tt>sed</tt>: Stream Editor



next up previous contents
Next: sort: Sorting a Up: Utilities Previous: grep: Global Regular

sed: Stream Editor

sed is like a non-interactive ed: it accepts ed commands, but never changes the contents of the input file:

sed -f (script_file) filename
or
sed -e (command) filename
where filename is the file on which command will act. Output is directed to the standard output.

-f
: what follows is the name of the file from which a series of commands will be taken.

NOTE: use script_file OR command (script_file is a file containing a list of commands).

The format of command is

[line[,line]]operation[parameter]

For example, to change the string cancer society to United Way in file letter.cancer, and store the output in file letter.united, one would use:

sed ``s/cancer society/United Way/" letter.cancer > letter.united

The next example is taken from [][p. 221]HM:UNIX. Pretend that file people contains a list of persons followed by an identification number. You want to:

To do that, a file, changes, is created, using:

prompt> cat > changes
/Sally/s/Smith/White/
/Henry/d
$a
James Walker
^ D

The first line says: search for the first line containing Sally, then substitute White for Smith.

The second line searches for the line containing Henry, then deletes it.

Finally, the third line says: go to the end of the file ($), append what follows-when append is used, a has to be appended to each line, the last one. In this case, only one line is appended, so the is put at the end of the command line.

To make these changes, and save the changed file in new.people,

sed -f changes people > new.people

As a last example, to save lines 10 to 30 in a file called 20.people, changes would contain:

1,9d 21,$d
and to run:
sed -f changes people > 20.people

More information on sed can be found in [pp. 219-233]HM:UNIX.



next up previous contents
Next: sort: Sorting a Up: Utilities Previous: grep: Global Regular



Super-User
Fri Feb 17 15:55:40 EST 1995