简体   繁体   English

如何使用sed删除与模式匹配的行及其后的行?

[英]How to delete the line that matches a pattern and the line after it with sed?

I have a file that looks something like: 我有一个看起来像这样的文件:

good text
good text
FLAG bad text
bad text
good text
good text
good test
bad Text FLAG bad text
bad text
good text

I need to delete any line containing "FLAG" and I always need to delete the one line immediately following the "FLAG" line too. 我需要删除任何包含“ FLAG”的行, 而且我也总是需要立即删除“ FLAG”行之后的一行。

"FLAG" lines come irregularly enough that I can't rely on any sort of line number strategy. “ FLAG”行不规则地出现,以至于我不能依靠任何种类的行号策略。

Anyone know how to do this with sed? 有谁知道如何用sed做到这一点?

Using an extension of the GNU version of : 使用GNU版本的扩展:

sed -e '/FLAG/,+1 d' infile

It yields: 它产生:

good text
good text
good text
good text
good test
good text

This works, and doesn't depend on any extensions: 这有效,并且不依赖于任何扩展:

sed '/FLAG/{N
d
}' infile

N reads the next line into the pattern space, then d deletes the pattern space. N将下一行读入模式空间,然后d删除模式空间。

Here is one way with awk : 这是awk一种方法:

awk '/FLAG/{f=1;next}f{f=0;next}1' file

or 要么

awk '/FLAG/{getline;next}1' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM