Chapter 20. Pattern Matching
A number of Unix text-processing utilities let you search for, and in some cases change, text patterns rather than fixed strings. These utilities include the editing programs vi and Emacs, programming languages like Perl and Python, and the commands grep and egrep. Text patterns (formally called regular expressions) contain normal characters mixed with special characters (called metacharacters).
For more information on regular expressions, see Mastering Regular Expressions (O’Reilly & Associates, Inc.).
Filenames Versus Patterns
When you issue a command on the command line, special characters are seen first by the shell, and 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 transformed by the shell into:
$ grep Array.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.
Get Mac OS X in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.