As a metacharacter, the ampersand (
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
The following example surrounds a word with point-size requests:
Because backslashes are also replacement metacharacters,
two backslashes are necessary to output a single backslash.
& in the replacement string refers to
UNIX. If the input line is:
on the UNIX Operating System.
then 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
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.
- from O'Reilly & Associates' sed & awk, Chapter 5