sed replace last line matching pattern

后端 未结 14 2088
暗喜
暗喜 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:03

    It can also be done in perl:

    perl -e '@a=reverse<>;END{for(@a){if(/a/){s/a/c/;last}}print reverse @a}' temp > your_new_file
    

    Tested:

    > cat temp
    a
    b
    c
    a
    b
    > perl -e '@a=reverse<>;END{for(@a){if(/a/){s/a/c/;last}}print reverse @a}' temp
    a
    b
    c
    c
    b
    > 
    

提交回复
热议问题