sed仅最后一个匹配模式 [英] sed only the last match pattern
本文介绍了sed仅最后一个匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想对文本文件的最后一个匹配模式进行 sed
.
I would like to sed
only the last match pattern of a text file.
输入文件:
boy
boy
girl
boy
输出文件:
boy
boy
girl
boys
推荐答案
一种方法是反转文件,仅替换第一个匹配项,然后再次反转.
One method would be to reverse the file, replace only the first match, and then reverse it back again.
tac <file> | sed '1 s/boy/boys/' | tac | sponge <newfile>
tac将反向连接并打印文件".海绵将吸收标准输入并写入文件".它在debian上的"moreutils"包中.
tac will "concatenate and print files in reverse". sponge will "soak up standard input and write to a file". It's in the "moreutils" package on debian.
这篇关于sed仅最后一个匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文