awk perl grep模式匹配忽略

时间:2014-05-01 14:05:13

标签: perl awk while-loop grep

我有一个大约100,000行的文件,看起来更像是这样:

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.*"){

我希望能够查找某些模式并忽略我选择忽略的模式,例如,查找5555和3333并忽略整个字符串,只留下4444。我最初想的是:

awk '!/4444/ && /proto/{a=1} a; /{/{a=0}' 

但它没有功能。所以我说hrmm 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 个答案:

答案 0 :(得分: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;
     }
  • 当看到需要跳过的模式时,禁用该标志。
  • 您打印设置了标志的行。
  • 当您看到模式结束时启用该标志。

答案 1 :(得分:2)

您可以通过RS变量将记录分隔符设置为}

awk '!/4444/' RS='}' ORS='}' file
相关问题