bash – 在最后一次与sed匹配后附加一行

我们假设我有以下输入.
Header
thing0 some info
thing4 some info
thing4 some info
thing4 some info
thing2 some info
thing2 some info
thing3 some info

现在,我想能够像这样在“thing4”的最后一次成功比赛中追加“foo”.

Header
thing0 some info
thing4 some info
thing4 some info
thing4 some info
foo
thing2 some info
thing2 some info
thing3 some info

订单不一定得到保证,但此示例中的顺序编号只是为了表明在某些文本行之前有一个搜索的关键字,并且它们通常在输入中组合在一起,但不能保证.

使用单个awk,您可以:
awk 'FNR==NR{ if (/thing4/) p=NR; next} 1; FNR==p{ print "foo" }' file file

Header
thing0 some info
thing4 some info
thing4 some info
thing4 some info
foo
thing2 some info
thing2 some info
thing3 some info

早期解决方案:您可以使用tac awk tac:

tac file | awk '!p && /thing4/{print "foo"; p=1} 1' | tac

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...