需求:根據(jù)文件內(nèi)容到忽,取出現(xiàn)次數(shù)最多的前兩位ip
基本文件內(nèi)容查看:
$cat test.txt
210.242.125.35 adwords.google.com
212.188.10.167 www.google.com
193.192.250.158 images-pos-opensocial.googleusercontent.com
193.192.250.158 clients4.google.com
222.255.120.42 google.com
222.255.120.42 apis.google.com
193.192.250.158 clients2.google.com
193.192.250.158 clients3.google.com
64.233.181.49 mts0.google.com
64.233.181.49 maps.gstatic.com
僅輸出ip:
$cat test.txt|awk '{print $1}'
210.242.125.35
212.188.10.167
193.192.250.158
193.192.250.158
222.255.120.42
222.255.120.42
193.192.250.158
193.192.250.158
64.233.181.49
64.233.181.49
按字符串規(guī)則排序:
$cat test.txt|awk '{print $1}'|sort
193.192.250.158
193.192.250.158
193.192.250.158
193.192.250.158
210.242.125.35
212.188.10.167
222.255.120.42
222.255.120.42
64.233.181.49
64.233.181.49
去重計數(shù):
$cat test.txt|awk '{print $1}'|sort|uniq -c
4 193.192.250.158
1 210.242.125.35
1 212.188.10.167
2 222.255.120.42
2 64.233.181.49
按首列數(shù)字倒敘排列輸出:
sort -n表示計數(shù)從小到大輸出 sort -r表示倒序輸出
$cat test.txt|awk '{print $1}'|sort|uniq -c|sort -nr
4 193.192.250.158
2 64.233.181.49
2 222.255.120.42
1 212.188.10.167
1 210.242.125.35
取對應行數(shù):
$cat test.txt|awk '{print $1}'|sort|uniq -c|sort -nr|head -n 2
4 193.192.250.158
2 64.233.181.49
第二種去重計數(shù)方式:
采用awk數(shù)組的形式計數(shù),然后排序輸出內(nèi)容
$cat test.txt|awk '{a[$1]++}END{for(i in a) print a[i],i}'|sort -nr|head -n 2
4 193.192.250.158
2 64.233.181.49