本文介绍了删除多条线路 - 从" patternA"比赛,通过与QUOT第二次出现; patternB"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 SED 或类似工具,找到包含行的东西吗?,并删除该行及其后的所有行,直到包含网​​络

I'd like to use sed or similar tool to find a line containing "something?" and delete that line and all following lines up until the second occurrence of a line containing "fi".

示例...

如果我有以下文件 / somepath / somefile 包含:

If I have the following file /somepath/somefile containing:

...

# Test something?
if something
then
do something
if somethingelse
then
do somethingelse
fi
fi

...

和我想找到并删除所有这些行,从包含行的东西吗?通过包含第二个网络

and I'd like to find and remove all those lines, starting with the line containing "something?" through the line containing the second "fi"

我能够开始删除包含的东西吗?,通过使用该文件的末尾 SED -n行所有行?/事/ q,p ,但我不知道如何添加一个条件包含第二行后说停网​​络 。任何帮助是AP preciated。

I'm able to remove all lines starting with the line containing "something?" through the end of the file using sed -n '/something?/q;p' but I'm not sure how to add a condition to say stop after the second line containing "fi". Any help is appreciated.

推荐答案

您使用awk做到这一点:

You do this with Awk:

awk 'BEGIN { del=0 } /Test something?/ { del=2 } del<=0 { print } /fi/ { del -= 1 }' your-file

这篇关于删除多条线路 - 从&QUOT; patternA&QUOT;比赛,通过与QUOT第二次出现; patternB&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:27