sed replace last line matching pattern

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

    Another one:

    tr '\n' ' ' | sed 's/\(.*\)a/\1c/' | tr ' ' '\n'
    

    in action:

    $ printf "%s\n" a b a b a b | tr '\n' ' ' | sed 's/\(.*\)a/\1c/' | tr ' ' '\n'
    a
    b
    a
    b
    c
    b
    

提交回复
热议问题