使用 awk 打印除匹配范围模式外的所有内容 [英] Printing all contents EXCEPT matching range pattern using awk
问题描述
在 Awk 中,范围模式不是表达式,因此不能使用!"不是它.那么如何实现它(使用awk打印除匹配范围模式之外的所有内容)?
In Awk, the range pattern is not an expression, so canot use the "!" to not it. so how to implement it (Printing all contents EXCEPT matching range pattern using awk)?
例如
$cat 1.t
abd
hfdh
#
fafa
deafa
123
#
end
我想要的结果:
猫 1.t
abd
hfdh
end
我举了一个不恰当的例子.endpattern 应该与 startpattern 不同,因为我还没有对此进行测试.那是我的错.
I gave an impertinent example. the endpattern should be different with the startpattern because I just have not test this. That's My fault.
同时,我想以不同的方式操作范围模式和非范围模式.所以 sed 不是我的选择.
At the same time, I want to operate the range pattern and the not range pattern differently. So sed is not my choice.
推荐答案
你只是举了一个棘手的(我不知道我应该称之为好还是坏^_^)的例子.您的文本具有完全相同的 startpattern
和 endpattern
(#
)
you just gave a tricky (I don't know I should call it good or bad ^_^ ) example. Your text have exactly same startpattern
and endpattern
(#
)
我猜你正在寻找与 sed '/#/,/#/d'
或 sed -n '/#/,/#/!p' 相同的方式代码>
I guess you are looking for the same way as sed '/#/,/#/d'
or sed -n '/#/,/#/!p'
awk 中有一些类似(与 sed 不同)的地址模型.在手册页中有解释.我说不一样,你的例子很好.如果 start == end
awk 的地址模型将不起作用:
There is some similiar (not same as sed's) address model in awk. In man page there is explanation. I said not same, your example is good one. if start == end
the address model for awk won't work:
kent$ echo "abd
hfdh
#
fafa
deafa
123
#
end"|awk '/#/,/#/{next}1'
abd
hfdh
fafa
deafa
123
end
因为 awk 匹配同一行(再次检查手册页),但如果它们不同,请参见以下示例:
because awk matches the same line (again check man page) but if they are different, see this example:
kent$ echo "abd
hfdh
#
fafa
deafa
123
##
end"|awk '/#/,/##/{next}1'
abd
hfdh
end
它会给你想要的.所以如果是这种情况,你可以这样做:
it will give what you want. so if this is the case, you could just do:
awk '/start/,/end/{next}1'
是的,与 sed 的非常相似.
yes, quite similar as sed's one.
如果开始和结束真的一样,你想用awk做,你需要flag.
If the start and end are really same, you want to do it with awk, you need flag.
kent$ echo "abd
hfdh
#
fafa
deafa
123
#
end"|awk '/#/&&!f{f=1;next}f&&/#/{f=0;next}!f'
abd
hfdh
end
好吧,在示例中最好使用 ^#$
,但这不是重点.我希望这能回答你的问题.
well, in example better use ^#$
, but that is not the point. I hope this answers your question.
这篇关于使用 awk 打印除匹配范围模式外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!