UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 6.3 MetacharactersChapter 6
Pattern Matching
Next: 7. The Emacs Editor
 

6.4 Examples of Searching

When used with grep or egrep, regular expressions are 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 any delimiter works). Here are some example patterns:

PatternWhat does it match?
bagThe string bag.
^bagbag at beginning of line.
bag$bag at end of line.
^bag$bag as the only word on line.
[Bb]agBag or bag.
b[aeiou]gSecond letter is a vowel.
b[^aeiou]gSecond letter is a consonant (or uppercase or symbol).
b.gSecond 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, grep or 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.
[A-Z].*An uppercase letter, followed by zero or more characters.
[A-Z]*Zero or more uppercase letters.
[a-zA-Z]Any letter.
[^0-9A-Za-z]Any symbol (not a letter or a number).
egrep or awk patternWhat does it match?
[567]One of the numbers 5, 6, or 7.
five|six|sevenOne of the words five, six, or seven.
80[23]?868086, 80286, or 80386
compan(y|ies)company or companies
ex or vi patternWhat does it match?
\<theWords like theater or the.
the\>Words like breathe or the.
\<the\>The word the.
sed or grep patternWhat does it match?
0\{5,\}Five or more zeros in a row.
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}Social security number (nnn-nn-nnnn).

6.4.1 Examples of Searching and Replacing

The following examples 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 tab.

CommandResult
s/.*/( & )/Redo the entire line, but add parentheses.
s/.*/mv & &.old/Change a wordlist (one word per line) into mv commands.
/^$/dDelete blank lines.
:g/^$/dSame as previous, in ex editor.
/^[ tab]*$/dDelete blank lines, plus lines containing spaces or tabs.
:g/^[ tab]*$/dSame as previous, in ex editor.
s/  */ /gTurn one or more spaces into one space.
%s/  */ /gSame as previous, in ex editor.
:s/[0-9]/Item &:/Turn a number into an item label (on the current line).
:sRepeat the substitution on the first occurrence.
:&Same as previous.
:sgSame, but for all occurrences on the line.
:&gSame as previous.
:%&gRepeat the substitution globally.
:.,$s/Fortran/\U&/gChange word to uppercase, on current line to last line.
:%s/.*/\L&/Lowercase entire file.
:s/\<./\u&/gUppercase first letter of each word on current line. (Useful for titles.)
:%s/yes/No/gGlobally change a word to No.
:%s/Yes/~/gGlobally 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 MetacharactersUNIX in a Nutshell: System V EditionNext: 7. The Emacs Editor
6.3 MetacharactersBook Index7. 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