使用awk打印除匹配范围模式以外的所有内容 [英] Printing all contents EXCEPT matching range pattern using awk

查看:137
本文介绍了使用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

$cat 1.t

abd
hfdh
#  
fafa
deafa
123 
#
end

我想要的结果:

猫1.t

abd
hfdh
end

我举了一个无关紧要的例子.结束模式应该与开始模式有所不同,因为我只是没有对此进行测试.那是我的错.

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.

推荐答案

您刚刚举了一个棘手的(我不知道我应该称其为好还是坏^ _ ^)示例.您的文本具有完全相同的startpatternendpattern(#)

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'

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进行操作,则需要标记.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆