I'd like to use the search pattern in the output part of a sed command. For instance, given the following sed command:
sed '/HOSTNAME=/cHOSTNAME=fred' /etc/sysconfig/network
I'd like to replace the second instance of "HOSTNAME=" with some escape sequence that references the search term. Something like this:
# this doesn't actually work
sed '/HOSTNAME=/c\?=fred' /etc/sysconfig/network
Does anywone know if there's a way to do this or do I have to repeat the search term in the answer.
I know I can do something like this:
sed 's/\(HOSTNAME=\)/\1fred/' /etc/sysconfig/network
But this is subtly different from what I want -- for instance #HOSTNAME=zug will turn into #HOSTNAME=fred, but I don't want the leading "#". The first sed example takes care of cases like this.