
698
|
Chapter 7: Pattern Matching
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
and grep 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
as follows:
$ grep "[A-Z]*" chap[12]
Double quotes suffice in most cases, but single quotes are the safest bet, since the
shell does absolutely no expansions on single-quoted text.
Note also that in pattern matching, ? matches zero or one instance of a regular
expression; in filename expansion, ? matches a single character.
Metacharacters
Different metacharacters have different meanings, depending upon where they are
used. In particular, regular expressions used for searching through text (matching)
have one set of metacharacters, while the metacharacters used when processing
replacement text (such as in a text editor) have a different set. These sets also vary
somewhat per program. This section covers the metacharacters used for searching
and replacing, with descriptions of the variants in the different utilities.
Search Patterns
The characters in the following table have special meaning only in search patterns.
Character Pattern
. Match any single character except newline. Can match newline in awk.
* Match any number (or none) of the single character that immediately precedes it. The preceding ...