如何选择可以使用awk / SED多次出现两个标记之间的图案线条 [英] How to select lines between two marker patterns which may occur multiple times with awk/sed
本文介绍了如何选择可以使用awk / SED多次出现两个标记之间的图案线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 AWK
或 SED
我怎么能选择哪些是两个不同的标记图案之间发生线路?有可能是标有这些模式的多个部分。
Using awk
or sed
how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns.
例如:
假设该文件包含:
For example: Suppose the file contains:
abc
def1
ghi1
jkl1
mno
abc
def2
ghi2
jkl2
mno
pqr
stu
和起始模式是 ABC
和结束的模式是 MNO
所以,我需要输出为:
And the starting pattern is abc
and ending pattern is mno
So, I need the output as:
def1
ghi1
jkl1
def2
ghi2
jkl2
我使用SED一次匹配的模式:
I am using sed to match the pattern once:
sed -e '1,/abc/d' -e '/mno/,$d' <FILE>
是否有 SED
任何方式或 AWK
来做到这一点,直到文件的结尾?
Is there any way in sed
or awk
to do it repeatedly until the end of file?
推荐答案
AWK
总是帮助:
$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file
def1
ghi1
jkl1
def2
ghi2
jkl2
说明:
-
/ ABC /
匹配有这个文本行,以及/ MNO /
一样。 -
/ ABC / {标志= 1;接下来}
设置标志
当文本ABC
中找到。然后,它会跳过就行了。 -
/ MNO / {标志= 0}
取消设置标志
当文本MNO
中找到。 - 最后
标志
是默认操作的模式,这是打印$ 1,0
:如果标志
等于1的行打印出来。 /abc/
matches lines having this text, as well as/mno/
does./abc/{flag=1;next}
sets theflag
when the textabc
is found. Then, it skips the line./mno/{flag=0}
unsets theflag
when the textmno
is found.- The final
flag
is a pattern with the default action, which is toprint $0
: ifflag
is equal 1 the line is printed.
Explanation:
这篇关于如何选择可以使用awk / SED多次出现两个标记之间的图案线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文