home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam

## 6.3 Metacharacters

### 6.3.1 Search Patterns

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

Character Pattern
```. ```

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 n th 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.

Class Characters 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 ```

### 6.3.2 Replacement Patterns

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

Character Pattern
```\ ```

Turn off the special meaning of the character that follows.

```\ ``` n

Restore the text matched by the n th 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 ``` .

 6.2 Metacharacters, Listed by Unix Program 6.4 Examples of Searching