我有10万行的文件,看起来更加的少这样的:AWK Perl的grep的模式匹配忽略
if (uri=~"^proto:[+]*55555.*"){
rewritehostport("10.10.10.2:1337");
rewritehostport("10.20.30.2:2345");
sl_send_reply("302", "Redirect");
exit;
}
if (uri=~"^proto:[+]*4444.*"){
rewritehostport("10.10.10.2:1337");
rewritehostport("10.20.30.2:2345");
sl_send_reply("302", "Redirect");
exit;
}
if (uri=~"^proto:[+]*3333.*"){
rewritehostport("10.10.10.2:1337");
rewritehostport("10.20.30.2:2345");
sl_send_reply("302", "Redirect");
exit;
}
我寻找到选择性忽略的变量的方法(比如55555)与沿线,直到花括号}
awk '/proto/{a=1} a; /{/{a=0}' myfile.cfg
忽略中心件,但仍产生了开始部分:
if (uri=~"^proto:[+]*55555.*"){
我想能够寻找某些patte rns并忽略那些我选择忽略的,例如,找到5555和3333并忽略整个字符串,只剩下4444。我最初想到的东西是:
awk '!/4444/ && /proto/{a=1} a; /{/{a=0}'
但它的功能。所以我说人力资源管理模式的Perl循环:
if ($_[1] =~ /proto/) {
if ($_[6] =~ /\}/) {
print "something\n";
foreach (@_) {
print $_;
}
print "something\n";
}
}
Buttttttt ...这将并不总是可行的,因为有些行可能是:
if (uri=~"^proto:[+]*9999.*"){
rewritehostport("10.10.10.2:1337");
sl_send_reply("302", "Redirect");
exit;
}
转念一想:grep -wvf file_with_data_I_want_removed original_file >> new_file
但是,这违背了目的,因为我不得不创建file_with_data_I_want_removed
从本质上说,我想说:
for [ this list of numbers (55555, 3333) ]
go into this_file if_number_exists remove line with number along with everything until the nearest curly bracket while ignoring the other ones
done
if (uri=~"^proto:[+]*4444.*"){
rewritehostport("10.10.10.2:1337");
rewritehostport("10.20.30.2:2345");
sl_send_reply("302", "Redirect");
exit;
}
去尝试既您的建议和user000001的建议。这不是一次性的,我随机得到:删除8888或99999和11111 – munkeyoto
@munkeyoto当然,另一种解决方案也很好。我看到的唯一警告是在输出中添加了新的行,并为每个部分缺少了末尾大括号('}')。 –
@munkeyoto JS就在这里,因为我忘了设置ORS。我更新了我的答案。 – user000001