加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:14:05 所属栏目:安全 来源:网络整理
导读:我们假设我有以下输入. Headerthing0 some infothing4 some infothing4 some infothing4 some infothing2 some infothing2 some infothing3 some info 现在,我想能够像这样在“thing4”的最后一次成功比赛中追加“foo”. Headerthing0 some infothing4 some
我们假设我有以下输入.
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读