简体   繁体   中英

awk script for list comparison of number of occurrences of Key value pairs

I have 2 awk statements to count the number of occurrences of the keys in file 1 and file 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

I have to find a solution to find out the keys present in file1 not in file2 and also the Keys in file 1 having more than 1 occurrence in file2. Also, vice versa

The below snippet provides distinct keys in file 1 not in file2 and viceversa

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]
    }' 

File contents:

File 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

File 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

didn't test, but this should print those keys you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM