2014-05-01 40 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