{"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":"\n\n\n
\u5bfc\u8bfb<\/td>\nawk\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
2\u3001\u4e24\u4e2a\u6587\u4ef6\u5dee\u5f02\u5bf9\u6bd4<\/strong><\/div>\n
\r\n\u6587\u4ef6\u5185\u5bb9\uff1a\r\n# seq 1 5 > a\r\n# seq 3 7 > b\r\n\u627e\u51fab\u6587\u4ef6\u5728a\u6587\u4ef6\u76f8\u540c\u8bb0\u5f55\uff1a\r\n\u65b9\u6cd51\uff1a\r\n# awk 'FNR==NR{a[$0];next}{if($0 in a)print $0}' a b\r\n3\r\n4\r\n5\r\n# awk 'FNR==NR{a[$0];next}{if($0 in a)print FILENAME,$0}' a b\r\nb 3\r\nb 4\r\nb 5\r\n# awk 'FNR==NR{a[$0]}NR>FNR{if($0 ina)print $0}' a b\r\n3\r\n4\r\n5\r\n# awk 'FNR==NR{a[$0]=1;next}(a[$0]==1)' a b  # a[$0]\u662f\u901a\u8fc7b\u6587\u4ef6\u6bcf\u884c\u83b7\u53d6\u503c\uff0c\u5982\u679c\u662f1\u8bf4\u660e\u6709\r\n# awk 'FNR==NR{a[$0]=1;next}{if(a[$0]==1)print}' a b\r\n3\r\n4\r\n5\r\n\u65b9\u6cd52\uff1a\r\n# awk 'FILENAME==\"a\"{a[$0]}FILENAME==\"b\"{if($0 in a)print $0}' a b\r\n3\r\n4\r\n5\r\n\u65b9\u6cd53\uff1a\r\n# awk 'ARGIND==1{a[$0]=1}ARGIND==2 && a[$0]==1' a b\r\n3\r\n4\r\n5\r\n\u627e\u51fab\u6587\u4ef6\u5728a\u6587\u4ef6\u4e0d\u540c\u8bb0\u5f55\uff1a\r\n\u65b9\u6cd51\uff1a\r\n# awk 'FNR==NR{a[$0];next}!($0 in a)' a b\r\n6\r\n7\r\n# awk 'FNR==NR{a[$0]=1;next}(a[$0]!=1)' a b\r\n# awk'FNR==NR{a[$0]=1;next}{if(a[$0]!=1)print}' a b\r\n6\r\n7\r\n\u65b9\u6cd52\uff1a\r\n# awk'FILENAME==\"a\"{a[$0]=1}FILENAME==\"b\" && a[$0]!=1' a b\r\n\u65b9\u6cd53\uff1a\r\n# awk 'ARGIND==1{a[$0]=1}ARGIND==2 && a[$0]!=1' a b\r\n<\/pre>\n
3\u3001\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6<\/strong><\/div>\n
\r\n\u6587\u4ef6\u5185\u5bb9\uff1a\r\n# cat a\r\nzhangsan 20\r\nlisi 23\r\nwangwu 29\r\n# cat b\r\nzhangsan man\r\nlisi woman\r\nwangwu man\r\n\u5c06a\u6587\u4ef6\u5408\u5e76\u5230b\u6587\u4ef6\uff1a\r\n\u65b9\u6cd51\uff1a\r\n# awk 'FNR==NR{a[$1]=$0;next}{print a[$1],$2}' a b\r\nzhangsan 20 man\r\nlisi 23 woman\r\nwangwu 29 man\r\n\u65b9\u6cd52\uff1a\r\n# awk 'FNR==NR{a[$1]=$0}NR>FNR{print a[$1],$2}' a b\r\nzhangsan 20 man\r\nlisi 23 woman\r\nwangwu 29 man\r\n\u5c06a\u6587\u4ef6\u76f8\u540cIP\u7684\u670d\u52a1\u540d\u5408\u5e76\uff1a\r\n# cat a\r\n192.168.1.1: httpd\r\n192.168.1.1: tomcat\r\n192.168.1.2: httpd\r\n192.168.1.2: postfix\r\n192.168.1.3: mysqld\r\n192.168.1.4: httpd\r\n# awk 'BEGIN{FS=\":\";OFS=\":\"}{a[$1]=a[$1] $2}END{for(v in a)print v,a[v]}' a\r\n192.168.1.4: httpd\r\n192.168.1.1: httpd tomcat\r\n192.168.1.2: httpd postfix\r\n192.168.1.3: mysqld\r\n\r\n\u89e3\u8bfb\uff1a\r\n\u6570\u7ec4a\u5b58\u50a8\u662f$1=a[$1] $2\uff0c\u7b2c\u4e00\u4e2aa[$1]\u662f\u4ee5\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u4e3a\u4e0b\u6807\uff0c\u503c\u662fa[$1] $2\uff0c\u4e5f\u5c31\u662f$1=a[$1] $2\uff0c\u503c\u7684a[$1]\u662f\u7528\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u4e3a\u4e0b\u6807\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\uff0c\u4f46\u7b2c\u4e00\u6b21\u6570\u7ec4a\u8fd8\u6ca1\u6709\u5143\u7d20\uff0c\u90a3\u4e48a[$1]\u662f\u7a7a\u503c\uff0c\u6b64\u65f6\u6570\u7ec4\u5b58\u50a8\u662f192.168.1.1=httpd\uff0c\u518d\u9047\u5230192.168.1.1\u65f6\uff0ca[$1]\u901a\u8fc7\u7b2c\u4e00\u5b57\u6bb5\u4e0b\u6807\u83b7\u5f97\u4e0a\u6b21\u6570\u7ec4\u7684httpd\uff0c\u628a\u5f53\u524d\u5904\u7406\u7684\u884c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u653e\u5230\u4e0a\u4e00\u6b21\u540c\u4e0b\u6807\u7684\u503c\u540e\u9762\uff0c\u4f5c\u4e3a\u4e0b\u6807192.168.1.1\u7684\u65b0\u503c\u3002\u6b64\u65f6\u6570\u7ec4\u5b58\u50a8\u662f192.168.1.1=httpd tomcat\u3002\u6bcf\u6b21\u9047\u5230\u76f8\u540c\u7684\u4e0b\u6807\uff08\u7b2c\u4e00\u4e2a\u5b57\u6bb5\uff09\u5c31\u4f1a\u83b7\u53d6\u4e0a\u6b21\u8fd9\u4e2a\u4e0b\u6807\u5bf9\u5e94\u7684\u503c\u4e0e\u5f53\u524d\u5b57\u6bb5\u5e76\u4f5c\u4e3a\u6b64\u4e0b\u6807\u7684\u65b0\u503c\u3002\r\n<\/pre>\n
4\u3001\u5c06\u7b2c\u4e00\u5217\u5408\u5e76\u5230\u4e00\u884c<\/strong><\/div>\n
\r\n# cat file\r\n1 2 3\r\n4 5 6\r\n7 8 9\r\n# awk '{for(i=1;i< =NF;i++)a[i]=a[i]$i\" \"}END{for(vin a)print a[v]}' file\r\n1 4 7\r\n2 5 8\r\n3 6 9\r\n\u89e3\u8bfb\uff1a\r\nfor\u5faa\u73af\u662f\u904d\u5386\u6bcf\u884c\u7684\u5b57\u6bb5\uff0cNF\u7b49\u4e8e3\uff0c\u5faa\u73af3\u6b21\u3002\r\n\u8bfb\u53d6\u7b2c\u4e00\u884c\u65f6\uff1a\r\n\u7b2c\u4e00\u4e2a\u5b57\u6bb5\uff1aa[1]=a[1]1\" \"  \u503ca[1]\u8fd8\u672a\u5b9a\u4e49\u6570\u7ec4\uff0c\u4e0b\u6807\u4e5f\u83b7\u53d6\u4e0d\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u6240\u4ee5\u4e3a\u7a7a\uff0c\u56e0\u6b64a[1]=1 \u3002\r\n\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\uff1aa[2]=a[2]2\" \"  \u503ca[2]\u6570\u7ec4a\u5df2\u7ecf\u5b9a\u4e49\uff0c\u4f46\u6ca1\u67092\u8fd9\u4e2a\u4e0b\u6807\uff0c\u4e5f\u83b7\u53d6\u4e0d\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u4e3a\u7a7a\uff0c\u56e0\u6b64a[2]=2 \u3002\r\n\u7b2c\u4e09\u4e2a\u5b57\u6bb5\uff1aa[3]=a[3]3\" \"  \u503ca[2]\u4e0e\u4e0a\u9762\u4e00\u6837\uff0c\u4e3a\u7a7a,a[3]=3 \u3002\r\n\u8bfb\u53d6\u7b2c\u4e8c\u884c\u65f6\uff1a\r\n\u7b2c\u4e00\u4e2a\u5b57\u6bb5\uff1aa[1]=a[1]4\" \"  \u503ca[2]\u83b7\u53d6\u6570\u7ec4a\u76842\u4e3a\u4e0b\u6807\u5bf9\u5e94\u7684\u503c\uff0c\u4e0a\u9762\u5df2\u7ecf\u6709\u8fd9\u4e2a\u4e0b\u6807\u4e86\uff0c\u5bf9\u5e94\u7684\u503c\u662f1\uff0c\u56e0\u6b64a[1]=1 4\r\n\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\uff1aa[2]=a[2]5\" \"  \u540c\u4e0a\uff0ca[2]=2 5\r\n\u7b2c\u4e09\u4e2a\u5b57\u6bb5\uff1aa[3]=a[3]6\" \"  \u540c\u4e0a\uff0ca[2]=3 6\r\n\u8bfb\u53d6\u7b2c\u4e09\u884c\u65f6\u5904\u7406\u65b9\u5f0f\u540c\u4e0a\uff0c\u6570\u7ec4\u6700\u540e\u8fd8\u662f\u4e09\u4e2a\u4e0b\u6807\uff0c\u5206\u522b\u662f1=1 4 7\uff0c2=2 5 8\uff0c3=36 9\u3002\u6700\u540efor\u5faa\u73af\u8f93\u51fa\u6240\u6709\u4e0b\u6807\u503c\u3002\r\n<\/pre>\n
5\u3001\u5b57\u7b26\u4e32\u62c6\u5206<\/strong><\/div>\n
\r\n\u5b57\u7b26\u4e32\u62c6\u5206\uff1a\r\n\u65b9\u6cd51\uff1a\r\n# echo \"hello\" |awk -F '''{for(i=1;i< =NF;i++)print $i}'\r\nh\r\ne\r\nl\r\nl\r\no\r\n\u65b9\u6cd52\uff1a\r\n# echo \"hello\" |awk '{split($0,a,\"''\");for(v in a)print a[v]}'\r\nl\r\no\r\nh\r\ne\r\nl\r\n<\/pre>\n
6\u3001\u7edf\u8ba1\u51fa\u73b0\u7684\u6b21\u6570<\/strong><\/div>\n
\r\n\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\uff1a\r\n# echo \"a.b.c,c.d.e\" |awk -F'[.,]' '{for(i=1;i< =NF;i++)a[$i]++}END{for(v in a)print v,a[v]}'\r\na 1\r\nb 1\r\nc 2\r\nd 1\r\ne 1\r\n<\/pre>\n
7\u3001\u8d39\u7528\u7edf\u8ba1<\/strong><\/div>\n
\r\n\u5f97\u51fa\u6bcf\u4e2a\u5458\u5de5\u51fa\u5dee\u603b\u8d39\u7528\u53ca\u6b21\u6570\uff1a\r\n# cat a\r\nzhangsan 8000 1\r\nzhangsan 5000 1\r\nlisi 1000 1\r\nlisi 2000 1\r\nwangwu 1500 1\r\nzhaoliu 6000 1\r\nzhaoliu 2000 1\r\nzhaoliu 3000 1\r\n# awk '{name[$1]++;cost[$1]+=$2;number[$1]+=$3}END{for(v in name)print v,cost[v],number[v]}' a\r\nzhangsan 5000 1\r\nlisi 3000 2\r\nwangwu 1500 1\r\nzhaoliu 11000 3\r\n<\/pre>\n
8\u3001\u83b7\u53d6\u67d0\u5217\u6570\u5b57\u6700\u5927\u6570<\/strong><\/div>\n
\r\n# cat a\r\na b 1\r\nc d 2\r\ne f 3\r\ng h 3\r\ni j 2\r\n\u83b7\u53d6\u7b2c\u4e09\u5b57\u6bb5\u6700\u5927\u503c\uff1a\r\n# awk 'BEGIN{max=0}{if($3>max)max=$3}END{print max}' a\r\n3\r\n\u6253\u5370\u7b2c\u4e09\u5b57\u6bb5\u6700\u5927\u884c\uff1a\r\n# awk 'BEGIN{max=0}{a[$0]=$3;if($3>max)max=$3}END{for(v in a)if(a[v]==max)print v}'a\r\ng h 3\r\ne f 3\r\n<\/pre>\n
9\u3001\u53bb\u9664\u6587\u672c\u7b2c\u4e00\u884c\u548c\u6700\u540e\u4e00\u884c<\/strong><\/div>\n
\r\n# seq 5 |awk'NR>2{print s}{s=$0}'\r\n2\r\n3\r\n4\r\n\r\n\u89e3\u8bfb\uff1a\r\n\u8bfb\u53d6\u7b2c\u4e00\u884c\uff0cNR=1\uff0c\u4e0d\u6267\u884cprint s\uff0cs=1\r\n\u8bfb\u53d6\u7b2c\u4e8c\u884c\uff0cNR=2\uff0c\u4e0d\u6267\u884cprint s\uff0cs=2 \uff08\u5927\u4e8e\u4e3a\u771f\uff09\r\n\u8bfb\u53d6\u7b2c\u4e09\u884c\uff0cNR=3\uff0c\u6267\u884cprint s\uff0c\u6b64\u65f6s\u662f\u4e0a\u4e00\u6b21p\u8d4b\u503c\u5185\u5bb92\uff0cs=3\r\n\u6700\u540e\u4e00\u884c\uff0c\u6267\u884cprint s\uff0c\u6253\u5370\u5012\u6570\u7b2c\u4e8c\u884c\uff0cs=\u6700\u540e\u4e00\u884c\r\n<\/pre>\n
10\u3001\u83b7\u53d6Nginx upstream\u5757\u5185\u540e\u7aefIP\u548c\u7aef\u53e3<\/strong><\/div>\n
\r\n# cat a\r\nupstream example-servers1 {\r\n   server 127.0.0.1:80 weight=1 max_fails=2fail_timeout=30s;\r\n}\r\nupstream example-servers2 {\r\n   server 127.0.0.1:80 weight=1 max_fails=2fail_timeout=30s;\r\n   server 127.0.0.1:82 backup;\r\n}\r\n# awk '\/example-servers1\/,\/}\/{if(NR>2){print s}{s=$2}}' a\r\n127.0.0.1:80\r\n# awk '\/example-servers1\/,\/}\/{if(i>1)print s;s=$2;i++}' a\r\n# awk '\/example-servers1\/,\/}\/{if(i>1){print s}{s=$2;i++}}' a\r\n127.0.0.1:80\r\n\r\n\u89e3\u8bfb\uff1a\r\n\u8bfb\u53d6\u7b2c\u4e00\u884c\uff0ci\u521d\u59cb\u503c\u4e3a0\uff0c0>1\u4e3a\u5047\uff0c\u4e0d\u6267\u884cprint s\uff0cx=example-servers1\uff0ci=1\r\n\u8bfb\u53d6\u7b2c\u4e8c\u884c\uff0ci=1\uff0c1>1\u4e3a\u5047\uff0c\u4e0d\u6267\u884cprints\uff0cs=127.0.0.1:80,i=2\r\n\u8bfb\u53d6\u7b2c\u4e09\u884c\uff0ci=2\uff0c2>1\u4e3a\u771f\uff0c\u6267\u884cprints\uff0c\u6b64\u65f6s\u662f\u4e0a\u4e00\u6b21s\u8d4b\u503c\u5185\u5bb9127.0.0.1:80\uff0ci=3\r\n\u6700\u540e\u4e00\u884c\uff0c\u6267\u884cprint s\uff0c\u6253\u5370\u5012\u6570\u7b2c\u4e8c\u884c\uff0cs=\u6700\u540e\u4e00\u884c\u3002\r\n\u8fd9\u79cd\u65b9\u5f0f\u4e0e\u4e0a\u9762\u4e00\u6837\uff0c\u53ea\u662f\u7528i++\u4f5c\u4e3a\u8ba1\u6570\u5668\u3002\r\n<\/pre>\n

\u638c\u63e1\u8fd9\u4e9bawk\u7528\u6cd5\u53ca\u601d\u8def\uff0c\u6211\u76f8\u4fe1\u4f60\u5728Linux\u4e0b\u5904\u7406\u6587\u672c\u65e0\u654c\u4e86\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u65e5\u5fd7\u683c\u5f0f\uff1a ‘$remote_addr – $remote_user [$time_local] “$requ […]<\/p>\n","protected":false},"author":63,"featured_media":151604,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-151600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/151600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=151600"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/151600\/revisions"}],"predecessor-version":[{"id":151747,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/151600\/revisions\/151747"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/151604"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=151600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=151600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=151600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}