gpt4 book ai didi

bash - 使用 sed 在匹配文本 block 后插入一行

转载 作者:行者123 更新时间:2023-11-29 09:06:54 25 4
gpt4 key购买 nike

我尝试使用 sed 在以下文件中的 [Block B] 之后插入一行:

[Block A]  
line 1
line 2

[Block B]
line 1
line 2

[Block C]
line 1
line 2

我使用的命令:

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

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

[Block B]  
line 1
line 2
inserted line

但是,我得到的是这个:

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

请告诉我如何使用 sed 获得所需的结果。谢谢!

最佳答案

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

关于bash - 使用 sed 在匹配文本 block 后插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11183469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com