start page | rating of books | rating of authors | reviews | copyrights

sed & awksed & awkSearch this book

4.2. A Global Perspective on Addressing

One of the first things you'll notice about sed commands is that sed will apply them to every input line. Sed is implicitly global, unlike ed, ex, or vi. The following substitute command will change every "CA" into "California."

s/CA/California/g

If the same command were entered from the ex command prompt in vi, it would make the replacement for all occurrences on the current line only. In sed, it is as though each line has a turn at becoming the current line and so the command is applied to every line. Line addresses are used to supply context for, or restrict, an operation. (In short: Nothing gets done in vi unless you tell it which lines to work on, while sed will work on every line unless you tell it not to.) For instance, by supplying the address "Sebastopol" to the previous substitute command, we can limit the replacement of "CA" by "California" to just lines containing "Sebastopol."

/Sebastopol/s/CA/California/g

An input line consisting of "Sebastopol, CA" would match the address and the substitute command would be applied, changing it to "Sebastopol, California." A line consisting of "San Francisco, CA" would not be matched and the substitution would not be applied.

A sed command can specify zero, one, or two addresses. An address can be a regular expression describing a pattern, a line number, or a line addressing symbol.

To illustrate how addressing works, let's look at examples using the delete command, d. A script consisting of simply the d command and no address produces no output since it deletes all lines:

d

When a line number is supplied as an address, the command affects only that line. For instance, the following example deletes only the first line:

1d

The line number refers to an internal line count maintained by sed. This counter is not reset for multiple input files. Thus, no matter how many files were specified as input, there is only one line 1 in the input stream.

Similarly, the input stream has only one last line. It can be specified using the addressing symbol $. The following example deletes the last line of input:

$d

The $ symbol should not be confused with the $ used in regular expressions, which means the end of the line.

When a regular expression is supplied as an address, the command affects only the lines matching that pattern. The regular expression must be enclosed by slashes (/). The following delete command

/^$/d

deletes only blank lines. All other lines are passed through untouched.

If you supply two addresses, then you specify a range of lines over which the command is executed. The following example shows hows to delete all lines blocked by a pair of macros, in this case, .TS and .TE, that mark tbl input.

/^\.TS/,/^\.TE/d

It deletes all lines beginning with the line matched by the first pattern and up to and including the line matched by the second pattern. Lines outside this range are not affected. The following command deletes from line 50 to the last line in the file:

50,$d

You can mix a line address and a pattern address:

1,/^$/d

This example deletes from the first line up to the first blank line, which, for instance, will delete a mailer header from an Internet mail message that you have saved in a file.

You can think of the first address as enabling the action and the second address as disabling it. Sed has no way of looking ahead to determine if the second match will be made. The action will be applied to lines once the first match is made. The command will be applied to all subsequent lines until the second match is made. In the previous example, if the file did not contain a blank line, then all lines would be deleted.

An exclamation mark (!) following an address reverses the sense of the match. For instance, the following script deletes all lines except those inside tbl input:

/^\.TS/,/^\.TE/!d

This script, in effect, extracts tbl input from a source file.

4.2.1. Grouping Commands

Braces ({}) are used in sed to nest one address inside another or to apply multiple commands at the same address. You can nest addresses if you want to specify a range of lines and then, within that range, specify another address. For example, to delete blank lines only inside blocks of tbl input, use the following command:

/^\.TS/,/^\.TE/{
      /^$/d
}

The opening curly brace must end a line and the closing curly brace must be on a line by itself. Be sure there are no spaces after the braces.

You can apply multiple commands to the same range of lines by enclosing the editing commands within braces, as shown below.

/^\.TS/,/^\.TE/{
	/^$/d
  	s/^\.ps 10/.ps 8/
  	s/^\.vs 12/.vs 10/
}

This example not only deletes blank lines in tbl input but it also uses the substitute command, s, to change several troff requests. These commands are applied only to lines within the .TS/.TE block.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.