开发者

sed: using search pattern in output

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开发者_JS百科=/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.


Try these:

sed 's/.*\(HOSTNAME=\)/\1fred/' /etc/sysconfig/network

Or

sed 's/.*\(HOSTNAME=\).*/\1fred/' /etc/sysconfig/network
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜