
702
|
Chapter 7: Pattern Matching
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Examples of Searching and Replacing
The examples in the following table show the metacharacters available to sed or
ex. Note that ex commands begin with a colon. A space is marked by a ❒; a tab is
marked by a tab.
\
[a-zA-Z] Any letter, either lower- or uppercase.
[^0-9A-Za-z] Any symbol or space (not a letter or a number).
[^[:alnum:]] Same, using POSIX character class.
egrep or awk pattern What does it match?
[567] One of the numbers 5, 6, or 7.
five|six|seven One of the words five, six, or seven.
80[2-4]?86 8086, 80286, 80386, or 80486.
80[2-4]?86|(Pentium(-III?)?) 8086, 80286, 80386, 80486, Pentium, Pentium-II, or Pentium-III.
compan(y|ies) company or companies.
ex or vi pattern What does it match?
\<the Words like theater or the.
the\> Words like breathe or the.
\<the\> The word the.
ed, sed or grep pattern What does it match?
0\{5,\} Five or more zeros in a row.
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} U.S. Social Security number (nnn-nn-nnnn).
\(why\).*\1 A line with two occurrences of why.
\([[:alpha:]_][[:alnum:]_.]*\) = \1; C/C++ simple assignment statements.
Command Result
s/.*/( & )/ Redo the entire line, but add spaces and parentheses.
s/.*/mv & &.old/ Change a wordlist (one word per line) into mv commands.
/^$/d Delete blank lines.
:g/^$/d Same as previous, ...