
697
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Chapter 7
7
Pattern Matching
A number of Linux text-processing utilities let you search for, and in some cases
change, text patterns rather than fixed strings. These utilities include the editing
programs ed, ex, vi, and sed; the gawk programming language; and the
commands grep and egrep. Text patterns (called regular expressions in computer
science literature) contain normal characters mixed with special characters (called
metacharacters).
This chapter presents the following topics:
• Filenames versus patterns
• Description of metacharacters
• List of metacharacters available to each program
• Examples
For more information on regular expressions, see the O’Reilly book Mastering
Regular Expressions.
Filenames Versus Patterns
Metacharacters used in pattern matching are different from metacharacters used
for filename expansion (see Chapter 6). However, several metacharacters have
meaning for both regular expressions and for filename expansion. This can lead to
a problem: the shell sees the command line first, and can potentially interpret an
unquoted regular expression metacharacter as a filename expansion. For example,
the command:
$ grep [A-Z]* chap[12]
could be transformed by the shell into:
$ grep Array.c Bug.c Comp.c chap1 chap2