sed is like a non-interactive ed: it accepts ed commands, but never changes the contents of the input file:
sed -f (script_file) filenamewhere filename is the file on which command will act. Output is directed to the standard output.
or
sed -e (command) filename
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,$dand to run:
sed -f changes people > 20.people
More information on sed can be found in [pp. 219-233]HM:UNIX.