ip统计实战案例
案例;统计access.log ip的次数并且降序出来
cat access.log
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/menu/papajohn.png?v=1.8 HTTP/1.1" 200 19872 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1"
111.172.170.44 - - [22/Nov/2015:12:01:01 +0800] "GET /online/api/mc/customer/orderContactInfo/list.json?customerId=12129521&showNumber= HTTP/1.1" 200 570 "http://www.papaonline.com.cn/online/oldboyonline/account/account.jsp" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
116.235.88.189 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/product/product_90765.png HTTP/1.1" 200 21717 "http://www.papaonline.com.cn/online/oldboyonline/product/index.jsp" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/forms/errorMsg-menu.png?v=1.8 HTTP/1.1" 200 837 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1"
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/quickAdd/bgQuickAddTop.png?v=1.8 HTTP/1.1" 200 490 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1"
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/quickAdd/bgQuickAddBody.png?v=1.8 HTTP/1.1" 304 0 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1"
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/quickAdd/bgQuickAddBottom.png?v=1.8 HTTP/1.1" 200 416 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1"
123.150.183.45 - - [22/Nov/2015:12:01:01 +0800] "GET /online/oldboyonline/images/quickAdd/arrow_down.png?v=1.8 HTTP/1.1" 200 525 "http://www.papaonline.com.cn/online/oldboyonline/order/orderNow.jsp" "Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SCH-N719 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4
看到题目肯定很乱没有头绪,经查看数据发现ip地址都在第一列,这时候我们可以用awk命令进行取列
awk ‘{print $1}‘ access.log
222.70.105.48
58.35.68.75
58.35.68.75
180.98.76.94
58.35.68.75
112.65.11.97
180.98.76.94
180.98.76.94
112.65.11.97
112.65.11.97
222.70.105.48
180.98.76.94
222.70.105.48
180.98.76.94
112.65.11.97
180.98.76.94
112.65.11.97
140.207.23.252
然后用sort命令进行统计排序
[root@oldboyedu41 tmp]# awk ‘{print $1}‘ access.log |sort -n|uniq -c|sort -rn|head
12049 58.220.223.62
10856 112.64.171.98
1982 114.83.184.139
1662 117.136.66.10
1318 115.29.245.13
961 223.104.5.197
957 116.216.0.60
939 180.111.48.14
871 223.104.5.202
869 223.104.4.139
sort -n 以数值排序
uniq -c 统计次数
sort -rn|head 以数值排序并且降序前10名
原文:http://chendao.blog.51cto.com/2895756/1975908