用sed匹配的文本块后插入一行 [英] use sed to insert a line after matching a block of text

查看:122
本文介绍了用sed匹配的文本块后插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用SED在下列文件中插入[B座]之后的一行:

  [A座]
1号线
2号线[B座]
1号线
2号线[C座]
1号线
2号线

我使用的命令:

  sed的'/ \\ [B座\\] /,/ ^ $ / A \\插入线文件

正确/期望的结果应该是:

  [B座]
1号线
2号线
插入线

不过,我得到这个代替:

  [B座]
插入线
1号线
插入线
2号线
插入线

请告诉我,我该怎么使用SED得到期望的结果。谢谢!


解决方案

  SED -e'/ \\ [B座\\] / {:一; N; / ^ $ / BA ; I \\插入线-e'}'

I try to use sed to insert a line after [Block B] in the following file:

[Block A]  
line 1  
line 2  

[Block B]  
line 1  
line 2  

[Block C]  
line 1  
line 2  

The command I used:

sed '/\[Block B\]/,/^$/a\inserted line' file

The correct/desired result should be:

[Block B]  
line 1  
line 2  
inserted line  

However, I got this instead:

[Block B]  
inserted line  
line 1  
inserted line  
line 2  
inserted line  

Please tell me how I can get the desired result using sed. Thanks!

解决方案

sed -e '/\[Block B\]/{:a;n;/^$/!ba;i\inserted line' -e '}'

这篇关于用sed匹配的文本块后插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆