Thursday, November 1, 2012

Using & as replacement for matched string in sed

This is useful, if you need to add some modification to your searched string in sed, but you are using regex and you do not know what the output would be. Please see below example:

$ echo "123 123 abc abc" | sed 's/[0-9]*/(&)/'
(123) 123 abc abc

In this example, your requirement is to put parentheses around your matched/searched string. So I put '&' as the replacement for the searched string, which is any series of number between 0 to 9, and put parentheses around it.

Done :)

No comments: