34.9 Referencing the Search String in a Replacement
As a metacharacter, the ampersand (
s/UNIX/\\s-2&\\s0/g
Because backslashes are also replacement metacharacters,
two backslashes are necessary to output a single backslash.
The
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
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 - from O'Reilly & Associates' sed & awk , Chapter 5 |
|