匹配两个数字文件超出范围并在范围

时间:2017-02-03 22:30:33

标签: unix awk numbers range match

执行:

awk -v FILE1="FILE1.out" -v FILE2="FILE2.out" '
     FNR==NR {BLs[++BSize]=$1;BHs[BSize]=$2;next}  {Blacked=0   for( i=1; i<BSize && ! Blacked; i++) Blacked = ( $2 >= BLs[i] && $2 <= BHs[i])
     if ( Blacked ) print > FILE2
      else print > FILE1
     }' BlackList_B_Number.dat 2OCS_VOICECALLS_1111_11_111111

使用这些输入文件:

cat BlackList_B_Number.dat 

902123123    902123124 
902123127    902123128 
 015                017 
450                600

cat 2OCS_VOICECALLS_1111_11_111111 

111111111 902123124    201605 
111111111 902123125    201605
 111111111 902123126   201605 
111111111 902123126    201605 
111111111 902123123    201605 
111111111 902123123    201605 
111111111 016    201605240 
111111111 1     20160524011
111111111 3    20160524024 
111111111 423   201605240 
111111111 5    20160524053 
111111111 470   201605240 
111111111 600   201605240 
111111111 400   424242432

FILE2应该有470和600,但不存在,它打印在FILE1上。

cat FILE2.out 
111111111    902123124    201605
111111111    902123123    201605 
111111111    902123123    201605 
111111111    016     201605240

1 个答案:

答案 0 :(得分:0)

使用预期结果进行轻微重写

$ awk 'NR==FNR {low[NR]=$1;hi[NR]=$2;n=NR;next} 
               {for(i=1;i<=n;i++) 
                  if($2>=low[i] && $2<=hi[i]) 
                    {print > "inrange"; next}
                print > "not_inrange"}' data calls



==> inrange <==
111111111 902123124    201605 
111111111 902123123    201605 
111111111 902123123    201605 
111111111 016    201605240 
111111111 470   201605240 
111111111 600   201605240 

==> not_inrange <==
111111111 902123125    201605
111111111 902123126   201605 
111111111 902123126    201605 
111111111 1     20160524011
111111111 3    20160524024 
111111111 423   201605240 
111111111 5    20160524053 
111111111 400   424242432