Section 7.4. Examples of Searching


7.4. Examples of Searching

When used with grep or egrep, regular expressions should be surrounded by quotes. (If the pattern contains a $, you must use single quotes; e.g., 'pattern'.) When used with ed, ex, sed, and awk, regular expressions are usually surrounded by /, although (except for awk) any delimiter works. The following tables show some sample patterns.

Pattern

What does it match?

bag

The string bag.

^bag

bag at the beginning of the line.

bag$

bag at the end of the line.

^bag$

bag as the only word on the line.

[Bb]ag

Bag or bag.

b[aeiou]g

Second letter is a vowel.

b[^aeiou]g

Second letter is a consonant (or uppercase or symbol).

b.g

Second letter is any character.

^...$

Any line containing exactly three characters.

^\.

Any line that begins with a dot.

^\.[a-z][a-z]

Same, followed by two lowercase letters (e.g., troff requests).

^\.[a-z]\{2\}

Same as previous; ed, grep, and sed only.

^[^.]

Any line that doesn't begin with a dot.

bugs*

bug, bugs, bugss, etc.

"word"

A word in quotes.

"*word"*

A word, with or without quotes.

[A-Z][A-Z]*

One or more uppercase letters.

[A-Z]+

Same; egrep or awk only.

[[:upper:]]+

Same as previous, egrep or awk.

[A-Z].*

An uppercase letter, followed by zero or more characters.

[A-Z]*

Zero or more uppercase letters.

[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.


7.4.1. 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

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, in ex editor.

/^[]*$/d

Delete blank lines, plus lines containing only spaces or tabs.

:g/^[]*$/d

Same as previous, in ex editor.

s/ *//g

Turn one or more spaces into one space.

:%s/ *//g

Same as previous, in ex editor.

:s/[0-9]/Item &:/

Turn a number into an item label (on the current line).

:s

Repeat the substitution on the first occurrence.

:&

Same as previous.

:sg

Same as previous, but for all occurrences on the line.

:&g

Same as previous.

:%&g

Repeat the substitution globally (i.e., on all lines).

:.,$s/Fortran/\U&/g

On current line to last line, change word to uppercase.

:.,$s/\(F\)\(ORTRAN\)/\1\L\2/g

On current line to last line, change spelling of "FORTRAN" to correct, modern usage.

:%s/.*/\L&/

Lowercase entire file.

:s/\<./\u&/g

Uppercase first letter of each word on current line. (Useful for titles.)

:%s/yes/No/g

Globally change a word to No.

:%s/Yes/~/g

Globally change a different word to No (previous replacement).


Finally, some sed examples for transposing words. A simple transposition of two words might look like this:

     s/die or do/do or die/     Transpose words

The real trick is to use hold buffers to transpose variable patterns. For example:

     s/\([Dd]ie\) or \([Dd]o\)/\2 or \1/  Transpose, using hold buffers



Linux in a Nutshell
Linux in a Nutshell
ISBN: 0596154488
EAN: 2147483647
Year: 2004
Pages: 147

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net