sed replace last line matching pattern

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

    Another approach:

    sed "`grep -n '^a$' a | cut -d \: -f 1 | tail -1`s/a/c/" a
    

    The advantage of this approach is that you run sequentially on the file twice, and not read it to memory. This can be meaningful in large files.

提交回复
热议问题