bash sed 多行删除模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37680636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed multiline delete with pattern
提问by Bastl
I want to delete all multiline occurences of a pattern like
我想删除一个模式的所有多行出现
{START-TAG
foo bar
ID: 111
foo bar
END-TAG}
{START-TAG
foo bar
ID: 222
foo bar
END-TAG}
{START-TAG
foo bar
ID: 333
foo bar
END-TAG}
I want to delete all portions between START-TAG and END-TAG that contain specific IDs.
我想删除包含特定 ID 的 START-TAG 和 END-TAG 之间的所有部分。
So to delete ID: 222 only this would remain:
所以要删除 ID: 222 只剩下这个:
{START-TAG
foo bar 2
ID: 111
foo bar 3
END-TAG}
{START-TAG
foo bar 2
ID: 333
foo bar 3
END-TAG}
I have a blacklist of IDs that should be removed.
我有一个应该删除的 ID 黑名单。
I assume a quite simple multiline sed regex script would do it. Can anyone help?
我假设一个非常简单的多行 sed 正则表达式脚本可以做到。任何人都可以帮忙吗?
It is very similar to Question: sed multiline replacebut not the same.
它与问题非常相似:sed multiline replace但不一样。
回答by andlrc
You can use the following:
您可以使用以下内容:
sed '/{START-TAG/{:a;N;/END-TAG}/!ba};/ID: 222/d' data.txt
Breakdown:
分解:
/{START-TAG/ { # Match '{START-TAG'
:a # Create label a
N # Read next line into pattern space
/END-TAG}/! # If not matching 'END-TAG}'...
ba # Then goto a
} # End /{START-TAG/ block
/ID: 222/d # If pattern space matched 'ID: 222' then delete it.