sed replace last line matching pattern

后端 未结 14 2109
暗喜
暗喜 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 01:59

    Here's another option:

    sed -e '$ a a' -e '$ d' file 
    

    The first command appends an a and the second deletes the last line. From the sed(1) man page:

    $ Match the last line.

    d Delete pattern space. Start next cycle.

    a text Append text, which has each embedded newline preceded by a backslash.

提交回复
热议问题