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

Book HomeBook TitleSearch this book

6.3. Metacharacters

6.3.1. Search Patterns

The characters in the following table have special meaning only in search patterns.

CharacterPattern
.

Match any single character except newline. Can match newline in awk.

*

Match any number (or none) of the single character that immediately precedes it. The preceding character can also be a regular expression; e.g., since . (dot) means any character, .* means “match any number of any character.”

^

Match the following regular expression at the beginning of the line or string.

$

Match the preceding regular expression at the end of the line or string.

[ ]

Match any one of the enclosed characters.

A hyphen (-) indicates a range of consecutive characters. A circumflex (^) as the first character in the brackets reverses the sense: it matches any one character not in the list. A hyphen or close bracket (]) as the first character is treated as a member of the list. All other metacharacters are treated as members of the list (i.e., literally).

{n,m}

Match a range of occurrences of the single character that immediately precedes it. The preceding character can also be a metacharacter. {n} matches exactly n occurrences, {n,} matches at least n occurrences, and {n,m} matches any number of occurrences between n and m. n and m must be between 0 and 255, inclusive.

\{n,m\}

Just like {n,m}, above, but with backslashes in front of the braces.

\Turn off the special meaning of the character that follows.
\( \)

Save the pattern enclosed between \( and \) into a special holding space. Up to nine patterns can be saved on a single line. The text matched by the subpatterns can be “replayed” in substitutions by the escape sequences \1 to \9.

\n

Replay the nth subpattern enclosed in \( and \) into the pattern at this point. n is a number from 1 to 9, with 1 starting on the left. See the following Examples.

\< \>

Match characters at beginning (\<) or end (\>) of a word.

+Match one or more instances of preceding regular expression.
?

Match zero or one instances of preceding regular expression.

|

Match the regular expression specified before or after.

( )

Apply a match to the enclosed group of regular expressions.

Many Unix systems allow the use of POSIX “character classes” within the square brackets that enclose a group of characters. These classes, listed here, are typed enclosed in [: and :]. For example, [[:alnum:]] matches a single alphanumeric character.

ClassCharacters Matched
alnum

Alphanumeric characters

alpha

Alphabetic characters

blank

Space or tab

cntrl

Control characters

digit

Decimal digits

graph

Nonspace characters

lower

Lowercase characters

print

Printable characters

space

Whitespace characters

upper

Uppercase characters

xdigit

Hexadecimal digits

6.3.2. Replacement Patterns

The characters in this table have special meaning only in replacement patterns.

CharacterPattern
\

Turn off the special meaning of the character that follows.

\n

Restore the text matched by the nth pattern previously saved by \( and \). n is a number from 1 to 9, with 1 starting on the left.

&

Reuse the text matched by the search pattern as part of the replacement pattern.

~

Reuse the previous replacement pattern in the current replacement pattern. Must be the only character in the replacement pattern. (ex and vi)

%

Reuse the previous replacement pattern in the current replacement pattern. Must be the only character in the replacement pattern. (ed)

\u

Convert first character of replacement pattern to uppercase.

\U

Convert entire replacement pattern to uppercase.

\l

Convert first character of replacement pattern to lowercase.

\L

Convert entire replacement pattern to lowercase.

\e, \E

Turn off previous \u, \U, \l, and \L.



Library Navigation Links

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