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 nonword 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 nonword-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.

Get Linux in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.