[Chapter 34] 34.10 Referencing Portions of a Search String
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:47:16Z

#

| Chapter 34 The sed Stream Editor |

## 34.10 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 26.4 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 `\1` and the second as `\2`, 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 13.11, 14.9, 16.6, 18.9, 45.30, and 51.3 have examples.

- DD from O'Reilly & Associates' sed & awk, Chapter 5

34.9 Referencing the Search String in a Replacement |  | 34.11 Search & Replacement: One Match Among Many