O'Reilly logo

Unix in a Nutshell, 4th Edition by Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Syntax of sed Commands

sed commands have the general form:

    [address [,address ]][!]command [arguments]

commands consist of a single letter or symbol; they are described later, by group and alphabetically. arguments include the label supplied to b or t, the filename supplied to r or w, and the substitution flags for s. addresses are described below.

Pattern Addressing

A sed command can specify zero, one, or two addresses. In POSIX sed, an address has one of the forms in the following table. Regular expressions are described in Chapter 7. Additionally, \n can be used to match any newline in the pattern space (resulting from the N command), but not the newline at the end of the pattern space.

Address

Meaning

/ pattern /

Lines that match pattern.

\; pattern ;

Like previous, but use semicolon as the delimiter instead of slash. Any character may be used. This is useful if pattern contains multiple slash characters.

N

Line number N.

$

The last input line.

If the command specifies:

Then the command is applied to:

No address

Each input line.

One address

Any line matching the address. Some commands accept only one address: a, i, r, q, and =.

Two comma-separated addresses

First matching line and all succeeding lines up to and including a line matching the second address.

An address followed by !

All lines that do not match the address.

GNU sed allows additional address forms:

Address

Meaning

/ pattern /i

Match pattern, ignoring case. I may be used instead of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required