匹配两个文件以超出范围内的数字

时间:2017-02-01 19:01:49

标签: unix awk numbers match

范围文件,例如(数字介于1 - 4和6 - 9之间):

  

BlackList_B_Number.dat

1     4
6     9

这是输入文件。例如:

  

OCS_VOICECALLS_1111_11_111111

111111111    1    201605240042070000000236OCS4252 3.6344   
111111111    2    201605240114430000000123OCS1154 03.0537  
111111111    3    201605240246070000000009OCS0707 018.6965 
111111111    4    201605240530500000000213OCS0130 01.8881  
111111111    5    201605240601120000000015OCS0156 015.8059 
111111111    6    201605240649100000000052OCS0269 02.3725  

我有兴趣从与黑名单输入文件相关的输入文件中过滤$ 2。我想得到这两个输出:

输出1(从过滤器输入记录):

111111111    5    201605240601120000000015OCS0156 015.8059 

Output2(过滤器中的输入记录):

111111111    1    201605240042070000000236OCS4252 3.6344   
111111111    2    201605240114430000000123OCS1154 03.0537  
111111111    3    201605240246070000000009OCS0707 018.6965 
111111111    4    201605240530500000000213OCS0130 01.8881  
111111111    6    201605240649100000000052OCS0269 02.3725 

我正在试验很多选项,但我只得到其中一个选项,而不仅仅是两个选项:

awk '
    FNR==NR{low[NR]=$1+0;hi[NR]=$2+0;next}
   {for(i in low)if($2>=low[i]&&$2<=hi[i]){print $0}}
' BlackList_B_Number.dat OCS_VOICECALLS_1111_11_111111

111111111    1    201605240042070000000236OCS4252 3.6344     1.5570     2.0773     0     000000    0     0JAVIE
111111111    2    201605240114430000000123OCS1154 03.0537    1.8992     1.1545     0     000000    0     0PPIB7
111111111    3    201605240246070000000009OCS0707 018.6965   18.4730    0.2235     0     000000    0     0PPXS8
111111111    4    201605240530500000000213OCS0130 01.8881    0.0000     1.8881     0     000000    0     0PPIB5
111111111    6    201605240649100000000052OCS0269 02.3725    1.7978     0.5746     0     000000    0     0PPIB7

我怎样才能同时获得另一个?:

111111111    5    201605240601120000000015OCS0156 015.8059 

提前致谢!!!

2 个答案:

答案 0 :(得分:1)

我会这样写:

filter="in"
awk -v filter="$filter" '
    NR == FNR {
        for (i=$1; i<=$2; i++) wanted[i]
        next
    }
    (filter == "in" && $2 in wanted) || (filter != "in" && !($2 in wanted))
' BlackList_B_Number.dat OCS_VOICECALLS_1111_11_111111

您可以使用shell变量$filter来控制。

我不明白你的上一个问题:如果你想要进出过滤器,请使用

cat OCS_VOICECALLS_1111_11_111111

答案 1 :(得分:0)

我想我刚刚得到它!!!!!:

滤波器=&#34;在&#34; awk -v filter =&#34; $ filter&#34; &#39; NR == FNR {for(i = $ 1; i&lt; = $ 2; i ++)想要[i]; next}(想要$ 2){print&gt; &#34; FILE1&#34;}!(想要$ 2){print&gt; &#34; FILE2&#34;}&#39; BlackList_B_Number.dat OCS_VOICECALLS_1111_11_111111

cat FILE1 111111111 1 201605240042070000000236OCS4252 3.6344
111111111 2 201605240114430000000123OCS1154 03.0537 111111111 3 201605240246070000000009OCS0707 018.6965 111111111 6 201605240530500000000213OCS0130 01.8881 111111111 4 201605240601120000000015OCS0156 015.8059

cat FILE2 111111111 5 201605240649100000000052OCS0269 02.3725