[Next] [Previous] [Up] [Top] [Contents]

7.2 Text Processing Commands

7.2.2 sed

The non-interactive, stream editor, sed, edits the input stream, line by line, making the specified changes, and sends the result to standard output.

Syntax

sed [options] edit_command [file]

The format for the editing commands are:

[address1[,address2]][function][arguments]

where the addresses are optional and can be separated from the function by spaces or tabs. The function is required. The arguments may be optional or required, depending on the function in use.

Line-number Addresses are decimal line numbers, starting from the first input line and incremented by one for each. If multiple input files are given the counter continues cumulatively through the files. The last input line can be specified with the "$" character.

Context Addresses are the regular expression patterns enclosed in slashes (/).

Commands can have 0, 1, or 2 comma-separated addresses with the following affects:

# of addresses lines affected

0 every line of input

1 only lines matching the address

2 first line matching the first address and all lines until, and including, the line matching the second address. The process is then repeated on subsequent lines.

Substitution functions allow context searches and are specified in the form:

s/regular_expression_pattern/replacement_string/flag

and should be quoted with single quotes (') if additional options or functions are specified. These patterns are identical to context addresses, except that while they are normally enclosed in slashes (/), any normal character is allowed to function as the delimiter, other than <space> and <newline>. The replacement string is not a regular expression pattern; characters do not have special meanings here, except:

& substitute the string specified by regular_expression_pattern

\n substitute the nth string matched by regular_expression_pattern enclosed in '\(', '\)' pairs.

These special characters can be escaped with a backslash (\) to remove their special meaning.

Common Options

-e script edit script

-n don't print the default output, but only those lines specified by p or s///p functions

-f script_file take the edit scripts from the file, script_file

Valid flags on the substitution functions include:

d delete the pattern

g globally substitute the pattern

p print the line

Examples

This example changes all incidents of a comma (,) into a comma followed by a space (, ) when doing output:

% cat filey | sed s/,/,\ /g

The following example removes all incidents of Jr preceded by a space ( Jr) in filey:

% cat filey | sed s/\ Jr//g

To perform multiple operations on the input precede each operation with the -e (edit) option and quote the strings. For example, to filter for lines containing "Date: " and "From: " and replace these without the colon (:), try:

sed -e 's/Date: /Date /' -e 's/From: /From /'

To print only those lines of the file from the one beginning with "Date:" up to, and including, the one beginning with "Name:" try:

sed -n '/^Date:/,/^Name:/p'

To print only the first 10 lines of the input (a replacement for head):

sed -n 1,10p


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]