14.06.2014 Views

Unix Power Tools

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

32.21<br />

Table 32-9. Search pattern examples (continued)<br />

Pattern<br />

[^0-9A-Za-z]<br />

Any symbol (not a letter or a number).<br />

[567] One of the numbers 5, 6, or 7.<br />

Extended regular expression patterns<br />

five|six|seven<br />

One of the words five, six, or seven.<br />

80[23]?86 One of the numbers 8086, 80286, or 80386.<br />

compan(y|ies)<br />

\<br />

\<br />

One of the words company or companies.<br />

Words like theater or the.<br />

Words like breathe or the.<br />

The word the.<br />

0\{5,\} Five or more zeros in a row.<br />

[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} U.S. Social Security number (nnn-nn-nnnn).<br />

Examples of Searching and Replacing<br />

Table 32-10 shows the metacharacters available to sed or ex. (ex commands<br />

begin with a colon.) A space is marked by ˘; a TAB is marked by tab.<br />

Table 32-10. Search and replace commands<br />

Command<br />

s/.*/( & )/<br />

s/.*/mv & &.old/<br />

/^$/d<br />

:g/^$/d<br />

What does it match?<br />

Result<br />

Redo the entire line, but add parentheses.<br />

Change a word list into mv commands.<br />

Delete blank lines.<br />

ex version of previous.<br />

/^[˘tab]*$/d<br />

Delete blank lines, plus lines containing only spaces or<br />

TABs.<br />

:g/^[˘tab]*$/d<br />

ex version of previous.<br />

s/˘˘*/˘/g<br />

Turn one or more spaces into one space.<br />

:%s/˘*/˘/g<br />

ex version of previous.<br />

:s/[0-9]/Item &:/<br />

Turn a number into an item label (on the current line).<br />

:s Repeat the substitution on the first occurrence.<br />

:&<br />

Same.<br />

:sg<br />

Same, but for all occurrences on the line.<br />

:&g<br />

Same.<br />

:%&g<br />

Repeat the substitution globally.<br />

:.,$s/Fortran/\U&/g<br />

Change word to uppercase, on current line to last line.<br />

:%s/.*/\L&/<br />

Lowercase entire file.<br />

Chapter 32: Regular Expressions (Pattern Matching) 655<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!