
by
Jeremy Canfield | Updated July 12th, 2018
Let's say you have two or more identical strings of data. In this example, the text "Hello" is found more than once.
Hello John Hello Amy Hello Sal
A simple sed statement will replace every occurrence of the world "Hello".
~]# echo "Hello John Hello Amy Hello Sal" | sed "s|Hello|Goodbye|"
Goodbye John Goodbye Amy Goodbye Sal
The following sed statement can be used to only replace the last occurrence of "Hello".
~]# echo "Hello John Hello Amy Hello Sal" | sed "s|\(.*\)Hello|\1Goodbye|"
Hello John Hello Amy Goodbye Sal