
Examples of Searching | 701
Pattern
Matching
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
In ed, ex, vi, and sed, the metacharacters in the following table are valid only in a
replacement pattern.
Examples of Searching
When used with grep or egrep, regular expressions should be 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 expressions are usually surrounded
by /, although (except for awk) any delimiter works. The following tables show
some sample patterns.
Symbol ex vi sed ed Action
\ ••••Escape following character.
\n ••••Text matching pattern stored in \( \).
& ••••Text matching search pattern.
~ • • Reuse previous replacement pattern.
% • Reuse previous replacement pattern.
\u \U • • Change character(s) to uppercase.
\l \L • • Change character(s) to lowercase.
\e • • Turn off previous \u or \l.
\E • • Turn off previous \U or \L.
Pattern What does it match?
bag The string bag.
^bag bag at the beginning of the line.
bag$ bag at the end of the line.
^bag$ bag as the only word on the line.
[Bb]ag Bag or bag.
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, ...