05. Backreferences and the Ampersand

Many times when we substitute a word, we want to make a reference to the phrase or part of the phrase that we just matched. For example, we may want to surround a query in parentheses or swap out two words we found. We can perform these substitutions using backreferences and the ampersand character.

Backreferences

Recall back to regular expressions that we can use backreferences to target a previously matched pattern. We capture these phrases by surrounding them by \( and \).

Later within the code, we may use \1 to recall the first pattern, \2 the second pattern, and so on for up to 9 patterns.

Example: switching two words around

To switch two words around, we create backreferences on two texts, and then references them with \1 and \2. In this example, we can see how to switch around any two words when the second is "them".

$ sed 's/\([a-z]*\) \(them\)/\2 \1/' oneOS.txt
# Extended regular expression version of the above
$ sed -E 's/([a-z]*) (them)/ \2 \1/' oneOS.txt
One OS to them rule all, One OS to them find. One OS to them call all, And in salvation them bind. In the bright land of Linux, Where the hackers play.

The above two commands perform the same task. However, the bottom one uses extended regular expressions, so does not need the \ character to escape parentheses.

& as the matched string

Sometimes you'll want to refer back to the entire matched text. For example, if you use a regex for the search term and want to surround that term with asterisks.

Example: wrapping around asterisks

To specify this, use the & character. This allows you to use whatever pattern matched in your replacement query.

For example, let's say you want to match all words that start with an uppercase vowels

$ sed 's/^[AEIOU][a-z]*/\*&\*/' oneOS.txt
*One* OS to rule them all, *One* OS to find them. *One* OS to call them all, *And* in salvation bind them. *In* the bright land of Linux, Where the hackers play.

To use the actual & sign, simply escape it with a backslash (\).

$ sed 's/and/\&/' file.txt

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad