[英]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做到这一点?
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.