Previous Table of Contents Next



BSD (Berkeley)
Some BSD versions do not support the n option of the substitute command.

p Write (print) the contents of the pattern space to the standard output if a replacement was made.
w wfile Append the contents of the pattern space to the file wfile if a replacement was made.
[2]t label If a substitution has occurred since the last input line was read or the last t command was performed, then branch to the :label statement. If label is empty then branch to the end of the script.
[2]w wfile Write (append) the pattern space to the file wfile. There can only be one space or tab between the w and the filename. You can have up to ten wfiles in a script.
[2]x Exchanges the contents of the hold space with the contents of the pattern space. The h and H commands place text in the hold space.
[2]y/str1/str2/ Transforms each character in str1 to the corresponding character in str2. For example,
y/abc/xyz/
changes every occurrence of an a to x, b to y, and c to z. The length of str1 and str2 must be identical.
[2]!command Apply the edit command to input lines that do not match the specified address. For example,
sed -n '/Total/!p' inputfile
displays all lines that do not match "Total."
:label The : (colon) signifies a label follows. The label provides a unique position in the script for the b and t commands to branch to, such as goto addresses.
[1]= Writes the current input line number to the standard output on a line by itself.
[2]{
 script
}
Execute the edit commands in script when an input line matches the specified address. The left brace ({) follows the given address, each command in script must be on a line by itself, and the right brace (}) must be on a line by itself. For example,
sed -n '/UNIX International/{
H
G
}
p' inputfile
This appends the pattern space to the hold space, then appends the hold space back to the pattern space. Thus each time the string address is matched, the number of times it is output increases. Useless, but an example nevertheless.
If a # (number sign) is the first character on the first line of a script file, then the entire line is ignored and can be used as a comment line. If the # is followed by an "n," #n, then all default output is suppressed. This is the same as using the -n option on the sed command line.
A blank line or empty command is ignored.

DIAGNOSTICS AND BUGS

The error messages returned by sed are usually difficult to understand and usually provide little help in understanding the problem. Typically, you have left out a quote, slash, or edit function letter and sed does not understand the syntax. The only help is to turn to the list of editing commands to make sure you are using the correct command syntax and to double check the placement of quotes and substitute delimiters (usually slash marks).

RELATED COMMANDS

Refer to the nawk, ed, and grep commands described in modules 6, 39, and 60.

RELATED FILES

The sed command reads input from a list of files or the standard input and writes the edited results to the standard output.

APPLICATIONS

The sed command is a useful shell script tool. Because it is a stream oriented editor it can be used in pipe commands, reading input from one command and passing output to another. It is also a batch type command, not requiring interactive input from a user. Thus the edit commands are all predefined and are applied to the input data upon execution.

The most common uses of sed are usually in shell scripts where the easiest way you can think of performing data manipulation is by using an editor. If you feel using an editor is the way to perform the changes, then use sed. This brings us to the flexibility of UNIX. Most tasks can be resolved using various commands. On one hand this is good but on the other it may seem confusing at first.

TYPICAL OPERATION

In this activity you use the sed command to display the contents of a file, substitute strings, and insert new text. Begin at the shell prompt.

1.  Type sed -n p mgmt and press Return. Notice that the file is displayed on your screen as if you had used the cat mgmt command.
2.  Type sed p mgmt and press Return. Now each line is displayed twice.
    cj> sed p mgmt
    Andrew
    Andrew
    Barbara
    Barbara
    Kathy
    Kathy
    Scott
    Scott

The -n option suppresses the normal output of every line. The p command prints the contents of the pattern space in addition to the default printing of it.
3.  Type sed 's/$/ (Manager)/' mgmt and press Return to change each occurrence of the end-of-line to " (Manager)."
4.  To add lines to the file, you use the i command. Type the following sed command, editing commands, followed by the text.

C Shell
If you are using the csh, type sh and press Return to enter a Bourne shell for the following example.

        cj> sed '3i\
        > Bill\
        > Leslie' mgmt<bold4del>
        Andrew
        Barbara
        Bill
        Leslie
        Kathy
        Scott
5.  Press Ctrl-D to return to your csh.
6.  Type sed 2q mgmt and press Return to display only the first two lines of the input file.
7.  Turn to Module 39 to continue the learning sequence.


Previous Table of Contents Next