[英]How can I do this in Awk? I have several files with two columns and identical values in first column. How to average values in second column by row?
[英]How to compare two columns of a row with same two columns of other row using awk? Can I also get the count the identical rows
Oct 7 02:55:20 123.111.44.77 99 123.111.66.91 12345 Oct 7 02:58:10 123.111.44.77 99 123.111.66.98 48575 Oct 7 02:05:40 123.111.44.77 99 123.111.66.99 09876 Oct 7 02:50:40 123.111.44.77 99 123.111.66.99 56743 Oct 7 02:53:00 123.111.44.77 99 123.111.66.12 98475 Oct 7 02:58:00 123.111.44.77 99 123.111.66.98 12489
這是輸入。
所需的輸出是:
123.111.44.77 123.111.66.99 (2) 123.111.44.77 123.111.66.98 (2)
如何使用AWK做到這一點?
先感謝您。
在AWK中,
$ awk '{c[$6]++}c[$6]>1{d[$6]=$4}END{for(x in d)printf "%s\t%s (%s)\n",d[x],x,c[x]}' infile
這產生
123.111.44.77 123.111.66.99 (2) 123.111.44.77 123.111.66.98 (2)
您可以調整輸出格式。 我用制表符分隔了前兩個字段,第二個和第三個字段(計數)用一個空格隔開。 您的樣本輸出看起來像這樣。
上面的代碼計算了右邊地址(第6列)的出現次數,並打印出從最后一次出現(連同括號中的計數)起不止一次出現的地址以及左邊地址(第4列)的出現。 進一步考慮,這可能並不是您想要的。 (如果是,那就太好了!)
如果要計算左右地址的配對 (第4列和第6列),請嘗試類似
$ awk '{c[$4"\t"$6]++}END{for(x in c)printf "%s (%d)\n",x,c[x]}' infile
這將打印每個配對,並在括號中標明出現的次數。 如果只希望配對不止一次,則添加if
條件:
$ awk '{c[$4"\t"$6]++}END{for(x in c)if (c[x]>1){printf "%s (%d)\n",x,c[x]}}' infile
請注意,所有這些都假定您想要的地址在第4列和第6列中。它們不執行任何正則表達式檢查等操作,以查看它們是否實際上看起來像地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.