O'Reilly logo

Mac OS X Tiger in a Nutshell by Jason McIntosh, Chuck Toporek, Chris Stone, Andy Lester

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. 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 ed, ex, vi, Emacs, and sed, the gawk scripting language, and the commands grep and egrep. Text patterns (also called regular expressions) contain normal characters mixed with special characters (called metacharacters).

Perl's regular expression support is so rich that it does not fit into this book; you can find a description in the O'Reilly books Mastering Regular Expressions, Perl in a Nutshell, Perl 5 Pocket Reference, or Programming Perl. The Emacs editor also provides regular expressions similar to those shown in this chapter.

ed and ex are hardly ever used as standalone, interactive editors nowadays. But ed can be found as a batch processor invoked from shell scripts, and ex commands are often invoked within vi through the colon (:) command. We use vi in this chapter to refer to the regular expression features supported by both vi and the ex editor on which it is based. sed and gawk are widely used in shell scripts and elsewhere as filters to alter text.

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]

can, for example, be transformed by the shell into:

$ grep Array.c ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required