Stream Editor - sed

Stream Editor – sed

The sed program is a text editor similar to ed (described in Chapter 11); unlike ed, sed is not interactive. Instead, sed is meant for editing a stream of input coming from standard input and for writing the edited text to standard output. The name sed stands for stream editor because it operates on a stream of text.

Cross Ref 

To read more about standard input and standard output, consult Chapter 7.

The sed editor is typically used to edit one or more files automatically through a shell script or to simplify repetitive edits on multiple files. The following sections provide an overview of sed.

Running sed

The sed editor expects to read from the standard input and write to the standard output. You can provide a filename on the command line, but sed still writes the output to standard output. This means that sed does not change the file it edits. You can redirect the output to another file or to the original file, thereby changing that file.

By default, sed applies each editing command globally to all lines in the file. You can, however, use line addresses or patterns to restrict the lines to which the editing commands apply.

To use sed on the fly from the shell prompt, you can run it as follows:

somecommand | sed 'editcommand' | anothercommand

In this case, feed the output of somecommand to sed (sed applies editcommand to each line); sed’s output goes to anothercommand. For example, to see all the current processes that have gnome in their name, you can type the following command, which uses sed to edit the stream:

ps ax | sed -n /gnome/p | cut -c 28-80
gnome-settings-daemon --oaf-activate-iid=OAFIID:GNOME
gnome-panel --sm-config-prefix /gnome-panel-9pyRb5/ -
gnome-terminal --sm-config-prefix /gnome-terminal-sHh
sed -n /gnome/p

In this case, each line in the output of the ps command is edited by sed, and a part of each line is extracted by using the cut command. Here, sed [i2]is invoked as follows:

sed -n '/gnome/p'

The -n option stops sed from printing each line to standard output. The '/gnome/p' part of the command line is an editing command—it means print each line containing the string gnome.

If you want to apply multiple sed commands, use the -e option to specify each editing command. For example, the following sed command takes the output of ps and deletes all lines that do not contain gnome (this is the edit command -e '/gnome/!d') and then deletes any line that contains the string sed (this is done by -e '/sed/d'):

ps ax | sed -e '/gnome/!d' -e '/sed/d' | cut -c 28-80
gnome-settings-daemon --oaf-activate-iid=OAFIID:GNOME
gnome-panel --sm-config-prefix /gnome-panel-9pyRb5/ -
gnome-terminal --sm-config-prefix /gnome-terminal-sHh

Of course, you typically do not type long sed commands at the shell prompt. Instead, sed is used in shell scripts. You can also place sed commands in a file and edit a text file by running sed with the -f option, as follows:

sed –f sedscriptfile myfile

This command edits myfile by using the sed commands in sedscriptfile, and writes the lines to standard output. If you want to save the output in another file, redirect the output as follows:

sed –f sedscriptfile myfile > editedfile

Learning the Basics of sed Commands

All sed commands have the following general form:


The parts shown in square brackets are optional. The command is an editing command (similar to those for the ed editor) that specifies the action sed takes on a line. The address specifies the lines to which the commands apply. The exclamation mark (!) applies the command to all lines that do not match the address. The arguments are needed by some editing commands, such as the w or r command, that read a file for which you have to provide the filename as an argument.

Cross Ref 

As you can see, the basic syntax of sed commands is simple; but the address usually includes regular expressions, which cause the sed commands to appear very cryptic. Chapter 24 describes regular expressions.

Sometimes you need to delete a line but to hold it in temporary storage and insert it elsewhere in the output stream. There are sed commands, such as h and g, for such operations. To use these commands, you need to understand the concept of pattern space and hold space. The pattern space is the buffer holding the current input line. The hold space is a separate set-aside buffer used for temporary storage. You can copy the contents of the hold space to the pattern space, and vice versa. For example, suppose that you want to extract all lines containing the string Item: and move them to a location later in the file (assume that the place is a line that has the string List of items:). You can do this with the following sed script:

/List of items:/G  

The first four lines of the script apply the commands H and d to the lines that contain Item:. The H command appends the line to the hold space; the d command deletes it. Then the G command on the last line of the script inserts the contents of the hold space after the string List of Items:.

Because of limited space, this section does not show you all the sed commands in detail. However, Table 8-6 provides a summary of commonly used sed commands, grouped by function.

Table 8-6: Commonly Used sed Commands






Prints the line number of a line. For example, /ORDER/= prints the line number of the lines containing the ORDER string (the line number is printed before each matching line).


Appends text after a line. For example, the following script appends a few lines to the file at the end of the output:

This line goes at the end of the file.\
Then this one.


Replaces text. For example, the following script replaces the first 50 lines of text with a single line that says <50 lines deleted>:

<50 lines deleted>


Inserts text before a line. For example, the following script inserts text before a line containing SKU 53055:

/SKU 53055/i\ 


Deletes lines. The command 1,20d deletes the first 20 lines.


Prints the line showing nonprinting characters as ASCII codes. For example, 5l prints the fifth line.


Skips the current line. For example, suppose a file contains section headers on a line right after a SECTION line. The following script extracts all section headers from the file (run this script with sed –n –f scriptfile, where scriptfile contains the following script):



Prints a line. For example, the /gnome/p command prints the lines containing the string gnome. Because sed prints each line anyway, this causes duplicate lines to be printed. You can use the p command with sed -n to print only selected lines (-n stops sed from printing lines by default).


Quits sed. For example, /Detailed Report/q quits the editor after printing the first line that contains this string.


Substitutes one pattern with another. For example, s/UNIX/Linux/ replaces all occurrences of UNIX with Linux.


Translates characters (similar to what the tr command does). For example, /^Appendix [1-9]/y/123456789/ABCDEFGHI/ replaces Appendix 1, 2, and so on, to Appendix A, B, and so on.

Working Multiple Lines



Deletes the first part of pattern space (up to a newline) created by the N command. For example, the following script deletes multiple blank lines:



Appends the next input line to the pattern space (a newline character separates the two lines). See the example for D.

Copying and Pasting



Copies the pattern space to the hold space, wiping out what was in the hold space. For example, the following script finds an order number (assumed to be four digits) in a file and prints a note followed by the order number:

s/Order \#[0-9]\{4\}$/We have shipped your order:/ 


Appends the pattern space to the hold space. See example earlier in the “Learning the Basics of sed Commands” section.

Copying and Pasting



Pastes the contents of the hold space into the pattern space, overwriting the previous contents


Pastes the contents of the hold space after the current line. See example earlier in the “Learning the Basics of sed Commands” section.


Exchanges contents of pattern space with the contents of hold space. See example for h.

File Input and Output



Reads a file into the input stream. For example, /^Dear/r letter inserts the contents of the letter file into the input stream after the line that begins with Dear.


Writes input lines to a file. For example, /^Order/w orders writes all lines that begin with Order to the file named orders.