2014-05-01 43 views
1

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

回答

2

您可以在記錄分隔,通過RS變量設置爲}

​​
3

你是非常接近的。重新安排標誌狀態應該可以獲得所需的輸出。

awk '/proto.*(55555|3333)/{a=0};a;/}/{a=1}' myfile.cfg 
    if (uri=~"^proto:[+]*4444.*"){ 
      rewritehostport("10.10.10.2:1337"); 
      rewritehostport("10.20.30.2:2345"); 
      sl_send_reply("302", "Redirect"); 
      exit; 
    } 
  • 時需要跳過你的模式是禁用的標誌。
  • 您可以打印設置了標誌的行。
  • 當您看到模式結束時啓用標誌。
+0

去嘗試既您的建議和user000001的建議。這不是一次性的,我隨機得到:刪除8888或99999和11111 – munkeyoto

+0

@munkeyoto當然,另一種解決方案也很好。我看到的唯一警告是在輸出中添加了新的行,併爲每個部分缺少了末尾大括號('}')。 –

+0

@munkeyoto JS就在這裏,因爲我忘了設置ORS。我更新了我的答案。 – user000001