Table 26-6. Special Characters in Search Patterns


What Does it Match?


Match any single character except newline.


Match any number (or none) of the single characters that immediately precede it. The preceding character can also be a regular expression. For example, since . (dot) means any character, means "match any number of any character."


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


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

[ ]

Match any one of the enclosed characters. A hyphen (-) indicates a range of consecutive characters. A caret (^) as the first character in the brackets reverses the sense: it matches any one character not in the list. A hyphen or a right square bracket (1) as the first character is treated as a member of the list. All other metacharacters are treated as members of the list.

\{n, m\}

Match a range of occurrences of the single character that immediately precedes it. The preceding character can also be a regular expression. \ (n\) will match exactly n occurrences; \ (n, \) will match at least n occurrences; and n, m\ ) will match any number of occurrences between n and m.


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. They can be "replayed" in substitutions by the escape sequences \ 1 to \9.

\< \>

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.


The characters in Table 26-7 have special meaning only in replacement patterns.

Table 26-7. Special Characters in Replacement Patterns


What Does it Match?


Turn off the special meaning of the character that follows.


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


Reuse the search pattern as part of the replacement pattern.


Reuse the previous replacement pattern in the current replacement pattern.


Convert first character of replacement pattern to uppercase.


Convert replacement pattern to uppercase.


Convert first character of replacement pattern to lowercase.


Convert replacement pattern to lowercase.

Examples of Searching

When used with grep or egrep, regular expressions are suffounde . d by quotes (if the pattern contains a $, you must use single quotes; e.g., 'pattern'.) When used with ed, ex, sed, and awk, regular expressions are usually Surrounded by / (although any delimiter works). Table 26-8 has some examples patterns.

Table 26-8. Search Pattern Examples


What Does It Match?


The string bag.


bag at beginning of line.


bag at end of line.


bag as the only word on line.


Bag or bag.


Second letter is a vowel.


Second letter is a consonant (or uppercase or symbol).


Second letter is any character.


Any line containing exactly three characters.


Any line that begins with a . (dot).


Same, followed by two lowercase letters (e.g., troff requests).


Same as previous, grep or sed only.


Any line that doesn't begin with a . (dot).


bug, bugs, bugss, etc.


A word in quotes.


A word, with or without quotes.


One or more uppercase letters.


Same, egrep or awk only.


An uppercase letter, followed by zero or more characters


Zero or more uppercase letters.


Any letter.


Any symbol (not a letter or a number).


One of the numbers 5, 6, or 7.


Table 26-8 Search Pattern Examples (continued)

egrep or awk pattern:


One of the words five, six, or seven.

80 [231 ?86

One of the numbers 8086, 80286, or 80386


One of the words company or companies.

ex or vi pattern:


Words like theater or the.


Words like breathe or the.


The word the.

sed or grep pattern:


Five or more zeros in a row.


US social security number (nnn-nn-nnnn).


Examples of Searching and Replacing

The following examples show the metacharacters available to sed or ex. (ex commands begin with a colon.) A space is marked by []; a TAB is marked by tab.

Table 26-9. Search and Replace Commands



s/.*/( & )/

Redo the entire line, but add parentheses.

s/.*/mv & &.old/

Change a woidlist into mv commands.


Delete blank lines.


ex version of previous.

/^[ tab]*$/d

Delete blank lines, plus lines containing only spaces or TABs.

:g/^[ tab]*$/d

ex version of previous.

s/ */ /g

Turn one or more spaces into one space.

:%s/ */ /9

exversion of previous

s/[0-9]/Item &:/

Turn a number into an item label (on the current line)


Repeat the substitution on the first occurrence.




Same, but for all occurrences on the line.




Repeat the substitution globally.


Change word to uppercase, on current line to line.


Lowercase entire file.


Uppercase first letter of each word on current line (useful for titles).


Globally change a word to No.


Globally change a different word to No (previous replacement).

s/die or do/do or die/

Transpose words.

s/\([Dd]ie\) or \([Dd]o\)/\2 or \1/

Transpose, using hold buffers to preserve case.