由統計ip出現次數展開的...

新來個intern,跟着某同事作devops,看其對於shell有點疑惑,因而列舉了個簡單的應用給他,統計IP出現次數 。shell

TCP    192.168.0.97:1052      60.28.216.15:80        CLOSE_WAIT
TCP    192.168.0.97:1436      219.133.60.243:8000    CLOSE_WAIT
TCP    192.168.0.97:1624      72.14.235.101:443      CLOSE_WAIT
TCP    192.168.0.97:1773      192.168.0.2:8000       ESTABLISHED
TCP    192.168.0.97:2181      207.46.110.37:1863     ESTABLISHED
TCP    192.168.0.97:2187      64.4.37.41:1863        ESTABLISHED
TCP    192.168.0.97:2188      207.46.27.162:1863     ESTABLISHED
TCP    192.168.0.97:2406      219.133.49.80:443      CLOSE_WAIT
TCP    192.168.0.97:4127      192.168.0.165:2120     ESTABLISHED
TCP    192.168.0.97:4142      192.168.0.165:2138     ESTABLISHED
TCP    192.168.0.97:4147      219.133.63.142:443     CLOSE_WAIT

統計第三列中ip出現的次數,並排序取前五個ipcode

shell能夠這麼搞:
排序

cat ip.txt |tr -s ' '| cut -d ' ' -f 3|cut -d : -f 1 | sort |uniq -c | head -n 5

帶awk能夠這麼搞:
ip

awk '{print $3}' ip.txt |cut -d : -f 1 | sort |uniq -c | head -n 5

要是沒有端口號的話,直接用awk也能完成統計:class

awk '{a[$4]++}END{for(i in a){if(a[i]>0){print a[i],i}}}' ip.txt |sort -nr | head -n 5
相關文章
相關標籤/搜索