用于列表比较键值对的出现次数的awk脚本

时间:2014-07-30 10:00:20

标签: bash shell awk

我有2个awk语句来计算文件1和文件2中键的出现次数

 awk  '{arr[$5"."$6]++}END{for (a in arr) {print a, arr[a]}}' file1
 649963661.3 1
 649963658.3 1
 649963660.3 1
 649963655.3 1


awk  '{arr[$5"."$6]++}END{for (a in arr) {print a, arr[a]}}' file2
 649963658.3 1
 649963660.3 2
 64963661.3 1
 649963655.8 1

我必须找到一个解决方案来找出file1中不存在于file2中的键以及file2中出现多于1次的文件1中的键。另外,反之亦然

以下代码段在文件1中提供不同的密钥,而不是在file2中,反之亦然

awk '{print $5"\t"$6}' file1 file2 | awk '
    !seen[$0]++{recs[++numRecs]=$0}
    {cnt[$0]++} 
    END{
      for (recNr=1;recNr<=numRecs;recNr++) 
        if (cnt[recs[recNr]] == 1) 
          print recs[recNr]
    }' 

文件内容:

档案1

Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963655       3       Fault_reg     0       0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963660       3       Fault_reg     0       0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963661       3       Fault_reg     0       0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963658       3       Fault_reg     0       0

文件2

Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963655       8       Fault_reg     0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963660       3       Fault_reg     0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963660       3       Fault_reg     0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  64963661       3       Fault_reg     0
Customer1    10.142.0.78     LSR001  Omnibus@147.128.231.7:  649963658       3       Fault_reg    0

1 个答案:

答案 0 :(得分:3)

没有测试,但这应该打印你想要的那些键:

awk '{k=$5"\t"$6}NR==FNR{a[k]++;next}{b[k]++}
     END{for(x in b)if(b[x]==1)delete a[x];for(x in a)print x}' f1 f2