Look at the parts of the sed command. We may be surprised for the replace string which is not the regular pattern.
Yes, at most of the time we may know the correct words to reaplace the pattern. But what should we do if we want to use part of the pattern to replace the string found? For example, double the key words. Now, Let's see the examples.
1.echo "Today is a good day." | sed 's/good/& &/g'
This string "good" will double the good words and separated with a blank space.
The word '&' corresponds the string found according to the pattern. And we can have any number of the '&' in the replace string.
2.echo "Today is a good day." | sed -r 's;([a-zA-Z]+day) is a ([a-zA-Z]+) (day);\1 and yesterday are \2 \3s;g'
Amazing? Yes! Sed is powerful! We can use the '\1' thru '\9' to keep parts of the pattern.
Now, some of you may find a serious problem. "Is sed recursive?" The answer is no. We do not have to worry about it. Just like the first example, we can use any number of '&' in the replace string.