English 中文(简体)
sed: using search pattern in output
原标题:
  • 时间:2009-11-24 19:08:12
  •  标签:
  • sed

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.

最佳回答

Try these:

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

Or

sed  s/.*(HOSTNAME=).*/1fred/  /etc/sysconfig/network
问题回答

暂无回答




相关问题
how to tell sed "dot match new line"

I can t figure how to tell sed dot match new line: echo -e "one two three" | sed s/one.*two/one/m I expect to get: one three instead I get original: one two three

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=/cHOSTNAME=fred /etc/sysconfig/network I d like to replace the ...

Text manipulation and removal

I have text files generated by one of my tools with structure shown below. 1 line text (space) multiple lines text (space) multiple lines text nr 2 ----------------------------------------------------...

Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract ...

Bulk Insert Code Before </body> Tag in 100 Files

I d like to insert <?php include_once( google_analytics.php ); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn t make a header or ...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: ...

热门标签