Section 10.3. Syntax of sed Commands


10.3. 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 next.

10.3.1. 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 i.

/pattern/m

Match pattern, allowing ^ and $ to match around an embedded newline. M may be used instead of m.

0,/pattern/

Similar to 1,/pattern/, but if line 1 matches pattern, it will end the range.

address,+n

Matches line matching address, and the n following lines.

address~incr

Matches line matching address and every incr lines after it. For example, 42~3 matches 42, 45, 48, and so on.


10.3.2. Pattern Addressing Examples

Command

Action performed

s/xx/yy/g

Substitute on all lines (all occurrences).

/BSD/d

Delete lines containing BSD.

/^BEGIN/,/^END/p

Print between BEGIN and END, inclusive.

/SAVE/!d

Delete any line that doesn't contain SAVE.

/BEGIN/,/END/!s/xx/yy/g

Substitute on all lines, except between BEGIN and END.


Braces ({ }) are used in sed to nest one address inside another or to apply multiple commands at a single matched address:

     [/pattern/[,/pattern/]]{     command1          command2     } 

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

10.3.3. GNU sed Regular Expression Extensions

With the -r option, GNU sed uses Extended Regular Expressions instead of Basic Regular expressions. (See Chapter 7 for more information.) However, even without -r, you can use additional escape sequences for more powerful text matching. The following escape sequences are valid only in regular expressions:


\b

Matches on a word boundary, where of the two surrounding characters (x\by), one is a word-constituent character and the other is not.


\B

Matches on a non-word boundary, where both of the two surrounding characters (x\by) are either word-constituent or not word-constituent.


\w

Matches any word-constituent character (i.e., a letter, digit, or underscore).


\W

Matches any non-word-constituent character (i.e., anything that is not a letter, digit, or underscore).


\`

Matches the beginning of the pattern space. This is different from ^ when the m modifier is used for a pattern or the s command.


\'

Matches the end of the pattern space. This is different from $ when the m modifier is used for a pattern or the s command.

The following escape sequences may be used anywhere.


\a

The ASCII BEL character.


\f

The ASCII formfeed character.


\n

The ASCII newline character.


\r

The ASCII carriage-return character.


\v

The ASCII vertical tab character.


\dnn

The character whose ASCII decimal value is nn.


\onn

The character whose ASCII octal value is nn.


\xnn

The character whose ASCII hexadecimal value is nn.



Linux in a Nutshell
Linux in a Nutshell
ISBN: 0596154488
EAN: 2147483647
Year: 2004
Pages: 147

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net