home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

6.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 example patterns.

6.4.1 General 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.

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


Second letter is any character.


Any line containing exactly three characters.


Any line that begins with a dot.


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


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


Any line that doesn't begin with a dot.


bug , bugs , bugss , etc.


A word in quotes.


A word, with or without quotes.


One or more uppercase letters.


Same; egrep or awk only.


Same; POSIX egrep or awk .


An uppercase letter, followed by zero or more characters.


Zero or more uppercase letters.

[a-zA-Z] Any letter.

Any symbol or space (not a letter or a number).


Same, using POSIX character class.

6.4.2 Egrep and awk patterns

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 .

8086 , 80286 , 80386 , or 80486 .


8086 , 80286 , 80386 , 80486 , Pentium , or Pentium-II .

compan(y|ies) company or companies .

6.4.3 Ex and vi patterns

ex or vi Pattern What Does It Match?
\<the Words like theater or the .
the\> Words like breathe or the .
\<the\> The word the .

6.4.4 Ed, sed and grep patterns

ed, sed or grep Pattern What Does It Match?

Five or more zeros in a row.


U.S. Social Security number (nnn -nn -nnnn ).


A line with two occurrences of why .

\([[:alpha:]_][[:alnum:]_.]*\) = \1;

C/C++ simple assignment statements.

6.4.5 Examples of Searching and Replacing

The examples in Table 6.3 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 .

Table 6.3: Searching and Replacing
Command Result
s/.*/( & )/

Redo the entire line, but add parentheses.

s/.*/mv & &.old/

Change a wordlist (one word per line) into mv commands.


Delete blank lines.


Same as previous, in ex editor.

/^[ tab ]*$/d

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

:g/^[ tab ]*$/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).


Repeat the substitution on the first occurrence.


Same as previous.


Same, but for all occurrences on the line.


Same as previous.


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


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


Lowercase entire file.


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


Globally change a word to No .


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

Previous: 6.3 Metacharacters UNIX in a Nutshell: System V Edition Next: 7. The Emacs Editor
6.3 Metacharacters Book Index 7. The Emacs Editor

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System