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

Linux in a NutshellLinux in a NutshellSearch this book

9.4. Examples of Searching

When used with grep or egrep, regular expressions are normally surrounded by quotes to avoid interpretation by the shell. (If the pattern contains a $, you must use single quotes, as in '$200', or escape the $, as in "\$200".) When used with ed, vi, sed, and gawk, regular expressions are usually surrounded by / (although any delimiter works). Here are some sample patterns:




The string "bag"


"bag" at beginning of line or string


"bag" at end of line or string


"bag" as the only text on line


"Bag" or "bag"


Second character is a vowel


Second character is not a vowel


Second character is any character except newline


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 (grep or sed only)


Any line that doesn't begin with a dot


"bug", "bugs", "bugss", etc


The string "word" in quotes


The string "word", with or without quotes


One or more uppercase letters


Same (egrep or gawk only)


An uppercase letter, followed by zero or more characters


Zero or more uppercase letters


Any letter


Any alphanumeric sequence

egrep or gawk pattern



One of the numbers 5, 6, or 7


One of the words five, six, or seven


8086, 80286, or 80386


company or companies

vi pattern



Words like theater or the


Words like breathe or the


The word the

sed or grep pattern



Five or more zeros in a row


Social security number (nnn-nn-nnnn)

9.4.1. Examples of Searching and Replacing

The following examples show the metacharacters available to sed and vi. We have shown vi commands with an initial colon because that is how they are invoked within vi. A space is marked by a ·; a tab is marked by tab.




Reproduce 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 vi editor.


Delete blank lines, plus lines containing spaces or tabs.


Same as previous, in vi editor.


Turn one or more spaces into one space.


Same as previous, in vi 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.


Change word to uppercase, on current line to last line.


Lowercase entire file.


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


Globally change a string ("yes") to another string ("No").


Globally change a different string to "No" (previous replacement).

Finally, here are 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

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.