10.5. Modifying strings with substitute (&)

If you want to be able to append or modify a string, you can use the ampersand (&) command. The ampersand stores the pattern found so that you can recall it and then place it in the replacement string. That was a bit of a mouthful; here’s the layout on the modifier. Give it a normal substitute pattern to search for, then a pattern you want to append the first pattern with, followed by an ampersand. When the modifier is placed it is placed before the matched pattern. For instance, this sed statement 's/nurse/"Hello" &/p' will produce the following output:

						$ sed -n 's/nurse/"Hello" &/p' quote.txt 
The local "Hello" nurse Miss P.Neave was in attendance.
					

based on the following input line of text, ‘The ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.