Vim search and replace - replace string that is found on particular lines only
So I have a bunch of lines like this -
$ns duplex-link n1 n2 10mb 10ms DropTail
$ns duplex-link-op n1 n2 10mb 10ms queuePos 0.5
$ns duplex-link n2 n3 10mb 10ms DropTail
$ns duplex-link-op n2 n3 10mb 10ms queuePos 0.5
$ns duplex-link n3 n4 10mb 10ms DropTail
$ns duplex-link-op n3 n4 10mb 10ms queuePos 0.5
Now here's the problem. I want the "10mb 10ms" string removed only where the second word happens to be "duplex-link-op". There开发者_StackOverflow社区fore I can't do a general replace "10mb 10ms" with a " " command. /- On a similar note, how do I do a search and replace of a particular string that happens to occur in a line that has another string? I am guessing it has something to do with backreferences ... but I am unable to find enough tutorials on the web on how to do so :(
:g/duplex-link-op/s/10mb 10ms//g
should replace them.
And try :help sub-replace-expression
, and :help sub-replace-special
for nearest thing to backreferences in Vim.
If you don't mind using other tools,
awk '$2=="duplex-link-op"{ sub("10mb 10ms","")}1' file > t && mv t file
sed -i.bak '/duplex-link-op/s/10mb 10ms//' file
精彩评论