sed replace last line matching pattern

后端 未结 14 2086
暗喜
暗喜 2020-12-11 01:45

Given a file like this:

a
b
a
b

I\'d like to be able to use sed to replace just the last line that contains an instance of \"a

14条回答
  •  無奈伤痛
    2020-12-11 02:11

    Here is all done in one single awk

    awk 'FNR==NR {if ($0~/a/) f=NR;next} FNR==f {$0="c"} 1' file file
    a
    b
    c
    b
    

    This reads the file twice. First run to find last a, second run to change it.

提交回复
热议问题