Pattern Matching

Most UNIX utilities support pattern matching (also called regular expressions). Rregular expressions contain normal characters mixed with special characters (also called metacharacters).
This section presents the following topics:
Metacharacters used in pattern matching are different from metacharacters used for filename expansion (see Sections 4 and 5). When you issue a command on the command line, special characters are seen first by the shell, then by the program; therefore, unquoted metacharacters are interpreted by the shell for filename expansion. The command:
$ grep [A-Z]* chap[12]
could, for example, be interpreted by the shell as:
$ grep Aray.c Bug.c Comp.c chap1 chap2
and would then try to find the pattern Array.c in files Bug.c, Comp.c, chap1, and chap2. To bypass the shell and pass the special characters to grep, use quotes:
$ grep "[A-Z]*" chap [12]
Double quotes suffice in most cases, but single quotes are the safest bet.
(Note also that in pattern matching, ? matches zero or one instance of a regular expression; in filename expansion, ? matches a single character.)
Metacharacters, Listed by UNIX Program
Some metacharacters are valid for one program but not for another. Those that are available to a UNIX program are marked by a bullet (.) in the table below. Fule descriptions are provided after the table.
In ed, ex, and sed, note that you specify both a search pattern (on the left) and a replacement pattern (on the right). The metacharacters abovwe are meaningful only in a search pattern.
In ed, ex, and se, the following additional metacharacters are valid only in a replacement pattern:
Metacharacters
The Characters below have special meaning only in search patterns:
. Match any single characters except newline.
* Match any number (or none) or 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.
$ 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 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.
\{n,m\} Match a reange of occurrences of the single character that immediately precedes it. The preceding character can also be a regular expresion. \{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 256m, inclusive.
\ Turn off the special meaning of the character that follows:
\( \) Save the pattern enclosed tetween \( 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 the regular expression specified before or after.
( ) Apply a match to the enclosed group of regular expressions.
The characters below have a special meaning only in replacement patterns.
\ Turn off the special meaning of the character that follows.
\n Restore the nth pattern previously saved by \( and \). N is a number from 1 to 9, with 1 starting on the left.
& Reuse the sewarch pattern as part of the replacement pattern.
~ Reuse the previous replacement pattern in the current replacement pattern.
\u Convert first character of replacement pattern to uppercase.
\U Convert replacement pattern to uppercase.
\l Convert first character of replacement pattern to lowercase.
\L Convert replacement pattern to lowercase. 
Examples of Searching
When used with egrep, regular expression s are surrounded by quotes. (If the pattern contains a $, you must use single quotes; e.g., 'pattern'.) When used with ed, ex, sed, and awk, regular expresions are usually surrounded by / (although any delimiter works). Here are some example patterns:
    bag
    The string bag
    ^bag
    bag at beginning of line.
    bag$
    bag at end of line.
    ^bag$
    bag as the only word on line.
    [Bb] ag
    Bag or bag in a line
    b[aeiou]g
    Second letter is a vowel.
    b[^aeiou]g
    Second letter is a consonant (or uppercase or symbol).
    b.g
    Second letter is any character.
    ^. . .$
    Any line containing exactly three characters.
    ^\.
    Any line that begins with a dot.
    ^\.[a-z] [a-z]
    Same, followed by two lower case letters (e.g., troff requests).
    ^\.[a-z]\{2\}
    Same as previous, grep or sed only.
    ^[^.]
    Any line that doesn't begin with a dot.
    bugs*
    Bug, bugs, buggs, etc.
    "word"
    A word in quotes.
    "*word"*
    A word, with or without quotes.
    [A-Z] [A-Z]*
    One or more upper case letters.
    [A-Z]+
    Same, egrep or awk only.
    [A-Z].*
    An uppercase letter, followed by zero or more characters.
    [A-Z]*
    Zero or more uppercase letters.
    [a-zA-Z]
    Any letter.
    [^0-9A-Za-z]
    Any symbol (not a letter or a number).