我有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