{"id":151600,"date":"2019-08-05T08:43:21","date_gmt":"2019-08-05T00:43:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=151600"},"modified":"2019-07-24T09:11:09","modified_gmt":"2019-07-24T01:11:09","slug":"awk-classic-case","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-classic-case.html","title":{"rendered":"\u5173\u4e8eAWK\u768410\u4e2a\u7ecf\u5178\u6848\u4f8b"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | awk\u662fLinux\u7cfb\u7edf\u4e0b\u4e00\u4e2a\u5904\u7406\u6587\u672c\u7684\u7f16\u7a0b\u8bed\u8a00\u5de5\u5177\uff0c\u80fd\u7528\u7b80\u77ed\u7684\u7a0b\u5e8f\u5904\u7406\u6807\u51c6\u8f93\u5165\u6216\u6587\u4ef6\u3001\u6570\u636e\u6392\u5e8f\u3001\u8ba1\u7b97\u4ee5\u53ca\u751f\u6210\u62a5\u8868\u7b49\u7b49\uff0c\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002 \n\u57fa\u672c\u7684\u547d\u4ee4\u8bed\u6cd5\uff1aawk option 'pattern {action}' file \n\u4e0b\u9762\u6839\u636e\u5de5\u4f5c\u7ecf\u9a8c\u603b\u7ed3\u4e8610\u4e2a\u5b9e\u7528\u7684awk\u6848\u4f8b\uff0c\u9762\u8bd5\u7b14\u8bd5\u9898\u4e5f\u7ecf\u5e38\u4f1a\u51fa\uff0c\u4f9b\u670b\u53cb\u4eec\u53c2\u8003\u5b66\u4e60\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n 1\u3001\u5206\u6790\u8bbf\u95ee\u65e5\u5fd7\uff08Nginx\u4e3a\u4f8b\uff09<\/strong><\/div>\n \r\n\u65e5\u5fd7\u683c\u5f0f\uff1a\r\n'$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\"'\r\n\r\n\u7edf\u8ba1\u8bbf\u95eeIP\u6b21\u6570\uff1a\r\n# awk '{a[$1]++}END{for(v in a)print v,a[v]}' access.log\r\n\u7edf\u8ba1\u8bbf\u95ee\u8bbf\u95ee\u5927\u4e8e100\u6b21\u7684IP\uff1a\r\n# awk '{a[$1]++}END{for(v ina){if(a[v]>100)print v,a[v]}}' access.log\r\n\u7edf\u8ba1\u8bbf\u95eeIP\u6b21\u6570\u5e76\u6392\u5e8f\u53d6\u524d10\uff1a\r\n# awk '{a[$1]++}END{for(v in a)print v,a[v]|\"sort -k2 -nr |head -10\"}' access.log\r\n\u7edf\u8ba1\u65f6\u95f4\u6bb5\u8bbf\u95ee\u6700\u591a\u7684IP\uff1a\r\n# awk'$4>=\"[02\/Jan\/2017:00:02:00\" &&$4< =\"[02\/Jan\/2017:00:03:00\"{a[$1]++}END{for(v in a)print v,a[v]}'access.log\r\n\u7edf\u8ba1\u4e0a\u4e00\u5206\u949f\u8bbf\u95ee\u91cf\uff1a\r\n# date=$(date -d '-1 minute'+%d\/%d\/%Y:%H:%M)\r\n# awk -vdate=$date '$4~date{c++}END{printc}' access.log\r\n\u7edf\u8ba1\u8bbf\u95ee\u6700\u591a\u768410\u4e2a\u9875\u9762\uff1a\r\n# awk '{a[$7]++}END{for(vin a)print v,a[v]|\"sort -k1 -nr|head -n10\"}' access.log\r\n\u7edf\u8ba1\u6bcf\u4e2aURL\u6570\u91cf\u548c\u8fd4\u56de\u5185\u5bb9\u603b\u5927\u5c0f\uff1a\r\n# awk '{a[$7]++;size[$7]+=$10}END{for(v ina)print a[v],v,size[v]}' access.log\r\n\u7edf\u8ba1\u6bcf\u4e2aIP\u8bbf\u95ee\u72b6\u6001\u7801\u6570\u91cf\uff1a\r\n# awk '{a[$1\" \"$9]++}END{for(v ina)print v,a[v]}' access.log\r\n\u7edf\u8ba1\u8bbf\u95eeIP\u662f404\u72b6\u6001\u6b21\u6570\uff1a\r\n# awk '{if($9~\/404\/)a[$1\" \"$9]++}END{for(i in a)print v,a[v]}' access.log\r\n<\/pre>\n |