As a metacharacter, the ampersand (&
) represents the extent of the
pattern match, not the line that was matched. For instance, you might use it to
match a word and surround it with troff
requests. The following example surrounds a word with point-size
requests:
s/UNIX/\\s-2&\\s0/g
Because backslashes are also replacement metacharacters, two backslashes are
necessary to output a single backslash. The &
in the replacement string refers to the string which was
originally matched, UNIX
. If the input line
is:
on the UNIX Operating System.
the substitute command produces:
on the \s-2UNIX\s0 Operating System.
The ampersand is particularly useful when the regular expression matches
variations of a word. It allows you to specify a variable replacement string
that corresponds to what was actually matched. For instance, let's say that you
wanted to surround with parentheses any cross reference to a numbered section in
a document. In other words, any reference such as See
Section 1.4
or See Section 12.9
should appear in parentheses, as (See Section
12.9)
. A regular expression can match the different combination of
numbers, so we use &
in the replacement
string and surround whatever was matched:
s/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/
The ampersand makes it possible to reference the entire match in the replacement string.
In the next example, the backslash is used to escape the ampersand, which appears literally in the replacement section:
s/ORA/O'Reilly \& Associates, Inc./g
It's easy to forget about the ampersand appearing literally in the replacement
string. If we had not escaped it in this example, the output would have been
O'Reilly ORA Associates, Inc
.
— DD