start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

17.14. Useful Global Commands (with Pattern Matches)

The best way to learn pattern matching is by example, so here's a short list of pattern-matching examples with explanations. (Section 32.21 has a list of these patterns.) Study the syntax carefully so you understand the principles at work. You should then be able to adapt these examples to your own situation.

  1. Change all occurrences of the word help (or Help) to HELP:

    % Section 20.3

    :%s/[Hh]elp/HELP/g

    or:

    :%s/[Hh]elp/\U&/g

    The \U changes the pattern that follows to all uppercase. The pattern that follows is the repeated search pattern, which is either help or Help.

  2. Replace one or more spaces following a colon (:) or a period (.) with two spaces (here a space is marked by a ):

    :%s/\([:.]\)*/\1/g

    Either of the two characters within brackets can be matched. This character is saved into a hold buffer, using \( and \) (Section 34.11) and restored on the right-hand side by the \1. Note that most metacharacters lose their special meanings inside brackets -- so the dot does not need to be escaped with a backslash (\).

  3. Delete all blank lines:

    g Section 20.4

    :g/^$/d

    What you are actually matching here is the beginning of the line (^), followed by the end of the line ($), with nothing in between.

  4. Delete all blank lines, plus any lines that contain only whitespace:

    :g/^[tab]*$/d

    (In the previous line, a TAB character is shown as tab.) A line may appear to be blank, but may in fact contain spaces or tabs. The previous numbered example will not delete such a line. This example, like the previous one, searches for the beginning and end of the line. But instead of having nothing in between, the pattern tries to find any number of spaces or tabs. If no spaces or tabs are matched, the line is blank. To delete lines that contain whitespace but that aren't blank, you would have to match lines with at least one space or tab:

    :g/^[tab][tab]*$/d
  5. This example and the next both refer to a line in a troff-formatted document like this A-level (top-level) heading macro call:

    .Ah "Budget Projections" "for 2001-2002"

    To match the first quoted argument of all section header (.Ah) macros and replace each line with this argument:

    :%s/^\.Ah "\([^"]*\)" .*/\1/

    this example macro call would be changed to simply:

    Budget Projections

    The substitution assumes that the .Ah macro can have more than one argument surrounded by quotes. You want to match everything between quotes, but only up to the first closing quote. As Section 32.18 explains, using ".*" would be wrong because it would match all arguments on the line. What you do is match a series of characters that aren't quotes, [^"]*. The pattern "[^"]*" matches a quote, followed by any number of nonquote characters, followed by a quote. Enclose the first argument in \( and \) so that it can be replayed using \1.

  6. Same as previous, except preserve the original lines by copying them:

    :g/^\.Ah/t$ | s/\.Ah "\([^"]*\)" .*/\1/

    In ex, the vertical bar (|) is a command separator that works like a semicolon (;) (Section 28.16) on a Unix command line. The first part, :g/^\.Ah/t$, matches all lines that begin with a .Ah macro, uses the t command to copy these lines, and places the copies after the last line ($) of the file. The second part is the same as in the previous example, except that the substitutions are performed on copies at the end of the file. The original lines are unchanged.

--TOR and DG , from Learning the vi Editor (O'Reilly, 1998)



Library Navigation Links

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