Referencing Portions of a Search String

In sed, the substitution command provides metacharacters to select any individual portion of a string that is matched and recall it in the replacement string. A pair of escaped parentheses are used in sed to enclose any part of a regular expression and save it for recall. Up to nine "saves" are permitted for a single line. n is used to recall the portion of the match that was saved, where n is a number from 1 to 9 referencing a particular "saved" string in order of use. (The section of article called "Remembering Patterns with \(, \), and \1" has more information.)

For example, to embolden the section numbers when they appeared as a cross reference, we could write the following substitution:

s/\(See Section \)\([1-9][0-9]*\.[1-9][0-9]*\)/\1\\fB\2\\fP/

Two pairs of escaped parentheses are specified. The first captures "See Section" (because this is a fixed string, it could have been simply retyped in the replacement string). The second captures the section number. The replacement string recalls the first saved substring as and the second as , which is surrounded by bold-font requests - for example, See Section \fB12.9\fP.

We can use a similar technique to match parts of a line and swap them. For instance, let's say there are two parts of a line separated by a colon. We can match each part, putting them within escaped parentheses and swapping them in the replacement:

% cat test1 first:second one:two % sed 's/\(.*\):\(.*\)/\2:\1/' test1 second:first two:one

The larger point is that you can recall a saved substring in any order, and multiple times.

Articles , , 16.6, , , and have examples.

- DD from Anonymous' sed & awk, Chapter 5