Lesson 7
Reuse Matched Text with sed’s Ampersand
Objective: Use & in replacement text without confusing it with shell syntax.
In the replacement part of an s instruction, an unescaped & represents the entire text matched by that substitution’s regular expression. It is replacement syntax, not a shell variable. In the shell, & is a background operator and $! expands to the most recent asynchronous process identifier; neither meaning describes sed’s replacement token.
sed 's/[0-9]\{5\}$/ID#&/' petchipThe pattern requires exactly five digits at the end of the line. The replacement inserts ID# followed by those matched digits. The first record therefore ends in ID#47823. The single quotes protect the complete script from shell interpretation.
Another useful detail is that a literal ampersand can be inserted into the replacement text by escaping it with a backslash. For example, sed 's/[0-9]\{5\}$/ID#\&/' petchip appends the characters ID#& to lines ending with five digits, rather than reproducing the matched digits. This distinction is important because & has a special meaning only within the replacement portion of the s command. When escaped as \&, it loses that special meaning and is treated as an ordinary character, allowing you to include a real ampersand in the output whenever needed.
Follow one substitution
- Read the next record into pattern space.
- Match the final five digits.
- Construct the replacement from ID# and the complete match.
- Print the modified record during normal output.
printf '%s\n' 'cat' | sed 's/cat/[&]/'
The output is [cat]. Ampersand can surround, duplicate, or annotate the match without spelling the matched text again. It refers to this substitution’s match, not a permanent variable shared across input lines.
Print a literal ampersand
printf '%s\n' 'cat' | sed 's/cat/cat \& kitten/'The backslash makes the replacement ampersand literal, producing cat & kitten. Without it, the matched text would be inserted at that position. Shell quoting and sed escaping solve different problems and both must be correct.
Captures versus the whole match
printf '%s\n' '123-456' | sed 's/\([0-9]*\)-\([0-9]*\)/\2:\1/'In a basic regular expression, escaped parentheses capture groups; back-references in the replacement select those groups. This example prints 456:123. Ampersand would select the complete 123-456 match instead. Keep the distinction clear before composing more complex substitutions.