{"id":880,"date":"2014-08-06T03:38:24","date_gmt":"2014-08-06T03:38:24","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=880"},"modified":"2014-08-06T03:38:24","modified_gmt":"2014-08-06T03:38:24","slug":"%e7%94%a8awk%e5%ae%9e%e7%8e%b0%e6%b1%82%e5%92%8c%e3%80%81%e5%9d%87%e5%80%bc%e3%80%81%e6%9c%80%e5%80%bc%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/880.html","title":{"rendered":"\u7528awk\u5b9e\u73b0\u6c42\u548c\u3001\u5747\u503c\u3001\u6700\u503c\u64cd\u4f5c"},"content":{"rendered":"<p>\u6ca1\u4e8b\u7684\u65f6\u5019\u559c\u6b22\u5728\u7f51\u4e0a\u901b\uff0c\u6240\u4ee5\u4e13\u95e8\u6ce8\u518c\u4e86\u4e2a\u53f7\u8ba2\u9605\u4e86\u5927\u91cf\u611f\u5174\u8da3\u7684RSS\uff0c\u4eca\u65e9\u5728\u901b\u7684\u65f6\u5019\u53d1\u73b0\u4e86\u4e2a\u611f\u5174\u8da3\u7684title\u201c<strong>Awk\u6c42\u6700\u5927\u503c\u6b63\u89e3<\/strong>\u201d\uff0c\u7136\u540e\u5c31\u70b9\u8fdb\u53bb\u770b\uff0c\u5728\u9a8c\u8bc1\u7684\u8fc7\u7a0b\u4e2d\u786e\u5b9e\u5b66\u5230\u4e86\u70b9\u4e1c\u897f\u3002<\/p>\n<p>\u6700\u5148\u5f00\u59cb\u770b\u5230\u6807\u9898\u7684\u65f6\u5019\u60f3\u4e86\u60f3\u7528awk\u8be5\u600e\u4e48\u5b9e\u73b0\uff0c\u601d\u8def\u7684\u8bdd\u7c7b\u4f3c\u4e8e\u505a\u4e00\u6b21\u5192\u6ce1\u6392\u5e8f\uff08\u611f\u89c9\u548c\u7b80\u5355\u9009\u62e9\u6392\u5e8f\u4e5f\u5f88\u50cf\uff1f\u6c42\u6307\u70b9\u3002\uff09\uff08\u5192\u6ce1\u6392\u5e8f\u5c31\u662f\u628a\u5c0f\u7684\u5143\u7d20\u5f80\u524d\u8c03\u6216\u8005\u628a\u5927\u7684\u5143\u7d20\u5f80\u540e\u8c03\uff0c\u6bd4\u8f83\u662f\u76f8\u90bb\u7684\u4e24\u4e2a\u5143\u7d20\u6bd4\u8f83\uff0c\u4ea4\u6362\u4e5f\u53d1\u751f\u5728\u8fd9\u4e24\u4e2a\u5143\u7d20\u4e4b\u95f4\u3002\uff09\u4f46\u8fd9\u91cc\u7684\u8bdd\u4e0d\u7528\u8fdb\u884c\u5143\u7d20\u7684\u4ea4\u6362\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u83b7\u53d6\u6700\u5927\/\u5c0f\u503c\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u6bd4\u8f83\u5373\u53ef\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cfmax\/min\u521d\u503c\u4e3a0\uff0c\u7136\u540e\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\u8fdb\u884c\u6bd4\u8f83\uff0c\u82e5max\/min\u6bd4\u5b83\u5927\/\u5c0f\uff0c\u5219\u5c06max\/min\u4fee\u6539\u4e3a\u8be5\u503c\u7684\u5927\u5c0f\uff0c\u5f53\u8fdb\u884c\u4e86NR\u6b21\u6bd4\u8f83\u4e4b\u540e\u5373\u53ef\u83b7\u53d6\u8be5\u5217\u7684\u6700\u503c\u3002<\/p>\n<p>\u601d\u8def\u5927\u6982\u5c31\u662f\u8fd9\u6837\uff0c\u4e0d\u8fc7\u6211\u8fd8\u662f\u5148\u53bb\u7f51\u4e0a\u627e\u7684\u4ee3\u7801\uff08\u4e0d\u77e5\u9053\u8fd9\u5230\u5e95\u662f\u4e2a\u597d\u4e60\u60ef\u8fd8\u662f\u574f\u4e60\u60ef\uff0c\u81ea\u5df1\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u7684\u8bdd\u53ef\u4ee5\u953b\u70bc\u81ea\u5df1\u7684\u601d\u7ef4\u80fd\u529b\u548c\u8c03\u8bd5\u80fd\u529b\uff0c\u800c\u4e14\u5370\u8c61\u4f1a\u6bd4\u8f83\u6df1\u523b\uff0c\u4e0d\u8fc7\u5c31\u662f\u201c\u6d6a\u8d39\u65f6\u95f4\u201d\uff0c\u56e0\u4e3a\u5f88\u591a\u65f6\u5019\u8fd9\u79cd\u95ee\u9898\u5728\u7f51\u4e0a\u662f\u80fd\u591f\u627e\u5230\u73b0\u6210\u7684\u89e3\u51b3\u65b9\u6848\u7684\uff0c\u5e76\u4e14\u522b\u4eba\u7684\u601d\u8def\/\u5b9e\u73b0\u4f1a\u6bd4\u4f60\u81ea\u5df1\u60f3\u51fa\u6765\u7684\u8981\u9ad8\u6548\u3002\u591a\u53c2\u8003\u522b\u4eba\u7684\u4ee3\u7801\/\u601d\u8def\u5728\u524d\u671f\u662f\u4e2a\u4e0d\u9519\u7684\u63d0\u9ad8\u65b9\u5f0f\uff0c\u4f46\u5230\u4e86\u540e\u671f\uff0c\u8fd8\u662f\u5f97\u81ea\u5df1\u60f3\u65b9\u8bbe\u6cd5\u53bb\u5b9e\u73b0\uff0c\u5426\u5219\u80fd\u529b\u8fd9\u65b9\u9762\u6c38\u8fdc\u8fd8\u662f\u63d0\u4e0d\u4e0a\u6765\uff0c\u5f97\u5b66\u4f1a\u521b\u9020\uff01\uff09<\/p>\n<p>\u6253\u5f00\u641c\u7d22\u5f15\u64ce\uff08Google\u88ab\u5c01\u4e86\u4e4b\u540e\u6211\u4e00\u822c\u7528Bing\uff0c\u6709\u65f6\u7528Baidu\u3001AOL\u3001Yahoo\u3001duckduckgo\uff0c\u5b9e\u5728\u627e\u4e0d\u5230\u4e86\u624d\u53bbGoogle\uff09\uff0c\u8f93\u5165\u5173\u952e\u5b57\u201cawk \u6c42\u5217\u7684\u6700\u5927\u503c\u201d\uff0c\u70b9\u5f00\u51e0\u4e2a\u94fe\u63a5\uff0c\u5927\u81f4\u7504\u522b\u4e86\u4e00\u4e0b\uff0c\u9009\u51fa\u51e0\u4e2a\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/www.2cto.com\/os\/201107\/97785.html\" target=\"_blank\">awk\u5b9e\u73b0\u6c42\u548c\u3001\u5e73\u5747\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u7684\u8ba1\u7b97\u64cd\u4f5c<\/a><\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/thread-1634049-1-1.html\" target=\"_blank\">awk\u6c42\u4e00\u5217\u6700\u5927\u503c<\/a><\/li>\n<li><a href=\"http:\/\/www.xiaoxiaozi.com\/2009\/11\/09\/1621\/\" target=\"_blank\">awk \u6700\u5927\u503c\uff0c\u6700\u5c0f\u503c \u5b57\u7b26\u4e32\u8f6c\u6570\u5b57<\/a><\/li>\n<\/ul>\n<p>\u5176\u4e2d\u7684\u7b2c\u4e00\u4e2a\u91cc\u9762\u5c31\u76f4\u63a5\u5305\u542b\u4e86\u201c\u6c42\u548c\u3001\u5e73\u5747\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u201d\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5148\u4e0d\u591a\u8bf4\uff0c\u5b9e\u9645\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n<p># cat calc<br \/>\n490898<br \/>\n1189235<br \/>\n20212<br \/>\n<span style=\"color: #ff0000;\"><strong>1494270<\/strong><\/span><br \/>\n146515<br \/>\n29369<br \/>\n23563<br \/>\n<span style=\"color: #0000ff;\"><strong>563027<\/strong><\/span><br \/>\n22976<br \/>\n127809<br \/>\n16813<br \/>\n551646<br \/>\n18858<br \/>\n18977<\/p>\n<pre class=\"lang:default decode:true \"># cat calc | awk 'BEGIN{max=0} {if(($1+0)&gt;max) max=($1+0) fi} END{print \"Max = \",max}'\nMax = 563027\n# cat calc | awk 'BEGIN{max=0} {if(($1+0)&gt;(max+0)) max=$1 fi} END{print \"Max = \",max}'\nMax = 1494270<\/pre>\n<p>\u56e0\u4e3a<span style=\"color: #ff0000;\"><strong>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cawk \u662f\u6309\u5b57\u7b26\u4e32\u65b9\u5f0f\u8fdb\u884c\u6bd4\u8f83\u7684<\/strong><\/span>\uff0c\u4f46\u6211\u4eec\u8fd9\u91cc\u662f\u60f3\u8ba9\u5176\u6309\u7167\u6570\u5b57\u7684\u65b9\u5f0f\u8fdb\u884c\u6bd4\u8f83\uff0c\u6240\u4ee5\u4e3a\u4e86\u80fd\u6b63\u786e\u83b7\u53d6\u5230\u6700\u5927\u503c\uff0c\u9700<span style=\"color: #ff0000;\">\u8981\u8ba9awk\u6309\u6570\u5b57\u7684\u65b9\u5f0f\u53bb\u6bd4\u8f83\uff0c\u5728\u6bd4\u8f83\u524d\u9700\u8981\u5148\u5c06\u53d8\u91cf\u5f3a\u5236\u8f6c\u6362\u4e3a\u6574\u6570\u578b<\/span>\uff0c\u8fd9\u6837\u83b7\u53d6\u5230\u7684\u6700\u5927\u503c\u624d\u4f1a\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p>awk\u00a0&#8216;BEGIN {max = 0} {if (<span style=\"color: #ff0000;\"><strong>$1+0<\/strong><\/span> &gt; <span style=\"color: #ff0000;\"><strong>max+0<\/strong><\/span>) max=$1 fi} END {print &#8220;Max=&#8221;,\u00a0max}&#8217;\u00a0data<\/p>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u5728\u6bd4\u8f83\u524d\u5bf9\u4e24\u4e2a\u53d8\u91cf\u5148\u52a00\uff08<span style=\"color: #0000ff;\"><strong>\u4e24\u4e2a\u53d8\u91cf\u9700\u8981\u540c\u65f6\u505a\u8f6c\u6362\uff0c\u4e0d\u7136\u4e5f\u5f97\u4e0d\u5230\u9884\u671f\u6548\u679c<\/strong><\/span>\uff09\uff0c\u5c31\u76f8\u5f53\u4e8e\u662f\u505a\u4ece\u5b57\u7b26\u4e32\u5230\u6570\u5b57\u7c7b\u578b\u7684\u8f6c\u6362\u64cd\u4f5c\u3002\u53ea\u6709\u5728\u6bd4\u8f83\u64cd\u4f5c\u7684\u65f6\u5019\u9700\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u679c\u53ea\u662f\u6c42\u548c\u7684\u8bdd\u5c31\u4e0d\u7528\u4e86\u3002\u4e0b\u9762\u653e\u4e0a\u8fd9\u51e0\u79cd\u64cd\u4f5c\u7684\u547d\u4ee4\u7ec4\u5408\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#1\u3001\u6c42\u548c\ncat data | awk '{sum+=$1} END {print \"Sum = \", sum}'\n\n#2\u3001\u6c42\u5e73\u5747\ncat data | awk '{sum+=$1} END {print \"Average = \", sum\/NR}'\n\n#3\u3001\u6c42\u6700\u5927\u503c\ncat data|awk 'BEGIN{max=0} {if ($1+0 &gt; max+0) max=$1 fi} END{print \"Max=\", max}'\n\n#4\u3001\u6c42\u6700\u5c0f\u503c\uff08min\u7684\u521d\u59cb\u503c\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u5927\u6570\u5373\u53ef\uff09\nawk 'BEGIN{min = 1999999} {if ($1+0 &lt; min+0) min=$1 fi} END{print \"Min=\", min}'\n\n#5\u3001\u6c42\u8bbf\u95ee\u6b21\u6570\u7684Top 10 Resource\uff0c\u53ef\u4ee5\u6839\u636e\u6b64\u8fdb\u884c\u4f18\u5316\ncat output\/logs\/cookie_logs\/`date +%u`\/cookie_log | grep -v '172.16' | grep -v '127.0.0.1' | awk -F' ' '{ if(index($1,\"219.141.246\")!=0) print $2; else print $1 }' | sort | uniq -c | sort -n | tail -10<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/xfeng.me\/awk-get-max\/\" target=\"_blank\">Awk\u6c42\u6700\u5927\u503c\u6b63\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/www.2cto.com\/os\/201107\/97785.html\" target=\"_blank\">awk\u5b9e\u73b0\u6c42\u548c\u3001\u5e73\u5747\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u7684\u8ba1\u7b97\u64cd\u4f5c<\/a><\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/thread-1634049-1-1.html\" target=\"_blank\">awk\u6c42\u4e00\u5217\u6700\u5927\u503c<\/a><\/li>\n<li><a href=\"http:\/\/www.xiaoxiaozi.com\/2009\/11\/09\/1621\/\" target=\"_blank\">awk \u6700\u5927\u503c\uff0c\u6700\u5c0f\u503c \u5b57\u7b26\u4e32\u8f6c\u6570\u5b57<\/a><\/li>\n<\/ul>\n<hr \/>\n<h6>\u5982\u4f55\u5c06\u4ece\u7f51\u9875\u4e2d\u590d\u5236\u7684\u5185\u5bb9\u8f93\u51fa\/\u590d\u5236\u5230Linux\u4e0b\u7684\u6587\u4ef6\u4e2d\uff1f<\/h6>\n<p>\u8fd9\u91cc\u6709\u51e0\u4e2a\u77e5\u8bc6\u70b9\uff1a<span style=\"color: #ff0000;\"><strong>\u91cd\u5b9a\u5411<\/strong><\/span>\u3001<span style=\"color: #ff0000;\"><strong>Here Document<\/strong><\/span><br \/>\n\u5b9e\u4f8b\u5982\u4e0b\uff1a<br \/>\n# cat &gt;calc &lt;&lt;EOF<br \/>\n&gt; 490898<br \/>\n&gt; 1189235<br \/>\n&gt; 20212<br \/>\n&gt; 1494270<br \/>\n&gt; 146515<br \/>\n&gt; 29369<br \/>\n&gt; 23563<br \/>\n&gt; 563027<br \/>\n&gt; 22976<br \/>\n&gt; 127809<br \/>\n&gt; 16813<br \/>\n&gt; 551646<br \/>\n&gt; 18858<br \/>\n&gt; 18977<br \/>\n&gt; EOF<\/p>\n<p># cat calc<br \/>\n490898<br \/>\n1189235<br \/>\n20212<br \/>\n1494270<br \/>\n146515<br \/>\n29369<br \/>\n23563<br \/>\n563027<br \/>\n22976<br \/>\n127809<br \/>\n16813<br \/>\n551646<br \/>\n18858<br \/>\n18977<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_73aca59701010pcs.html\" target=\"_blank\">http:\/\/blog.sina.com.cn\/s\/blog_73aca59701010pcs.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ca1\u4e8b\u7684\u65f6\u5019\u559c\u6b22\u5728\u7f51\u4e0a\u901b\uff0c\u6240\u4ee5\u4e13\u95e8\u6ce8\u518c\u4e86\u4e2a\u53f7\u8ba2\u9605\u4e86\u5927\u91cf\u611f\u5174\u8da3\u7684RSS\uff0c\u4eca\u65e9\u5728\u901b\u7684\u65f6\u5019\u53d1\u73b0\u4e86\u4e2a\u611f\u5174\u8da3\u7684title\u201c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[74,298],"class_list":["post-880","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-awk","tag-heredocument"],"views":2906,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}