Previous Table of Contents Next

Module 117


The external sed command is a stream editor. A stream editor reads input from a list of files or from the standard input, applies editing commands to each line of input and writes the resulting line to the standard output. The standard output by default is your terminal screen. The input data is often referred to as a stream (of data), thus the name "stream editor."

Some of the editing commands are derived from the ed editor. These commands can be specified on the command line or within a file. They can have addresses specified as line numbers or as patterns. The editing commands are referred to as the sed script.


Following is the general format of the sed command.

     sed [ -n ] [ -e script ] [ -f scriptfile ] [ file_list ]


The following list describes the options and their arguments that may be used to control how sed functions.

-n Suppresses the default action of writing every input line to the standard output.
-e script The script is a set of editing functions used to edit the input data. If you only specify one set of scripts and you do not specify the -f option, you may omit the -e. You usually have to enclose the scripts in single or double quotes to prevent interpretation by the shell. You can use one -e option and place multiple edit commands within quotes or you can use multiple sets of -e scripts to perform the edits. For example, using a single -e with multiple edit commands in a single script might look like the following:
sed -e 's/BEGIN/START/g
/^$/d' inputfile > outputfile
This applies each edit command (function) to each input line. The same effect can be accomplished using multiple -e options. For example,
sed -e 's/BEGIN/START/g' -e 's/NIGHT/DAY/g' \
-e '/^$/d' inputfile > outputfile
-f scriptfile Allows you to store an edit script in the file scriptfile. You can use multiple -f scriptfile options to have sed use more than one scriptfile. The edit functions are read from the scriptfile and applied to the input data stream.


The following describes the argument that may be passed to the sed command.

file_list The list of input files containing the data for editing.


The sed command processes one line of input at a time. It reads in a line and stores it in the temporary work buffer referred to as the pattern space. The address of each editing command is compared to the address or text of the pattern space for a match. If a match occurs, the edit command is applied to the text stored in the pattern space.

All edit commands in all sed scripts are applied to the text stored in the pattern space. Once all edit commands have been applied to the pattern space text, the pattern space is copied to the standard output and then deleted. The next line of input is read into the pattern space and the process is repeated. Thus sed processes each line of input in a cycle, reading input, performing all editing commands that apply, writing to the output, and clearing out the work area.

The text stored in the pattern space can be copied to a holding buffer called the hold space. The text in the hold space can later be copied back to the pattern space in part or whole. There are three edit commands that store text in the hold space; they are the h, H, and x commands.

Previous Table of Contents Next