{"id":641,"date":"2014-07-19T10:05:21","date_gmt":"2014-07-19T10:05:21","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=641"},"modified":"2014-07-19T10:05:21","modified_gmt":"2014-07-19T10:05:21","slug":"awk%e5%ad%a6%e4%b9%a0_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/641.html","title":{"rendered":"awk\u5b66\u4e60_2"},"content":{"rendered":"<div>\u4eca\u5929\u51c6\u5907\u5b9e\u9645\u6d4b\u8bd5\u4e00\u4e0b\u6628\u5929\u4fee\u6539\u8fc7\u7684\u201c<a title=\"\u81ea\u52a8\u5c4f\u853d\u4e00\u4e9b\u6076\u610fIP\" href=\"http:\/\/ixyzero.com\/blog\/archives\/411.html\" target=\"_blank\">\u81ea\u52a8\u5c4f\u853d\u4e00\u4e9b\u6076\u610fIP<\/a>\u201d\u7684\u6587\u7ae0\uff0c\u770b\u770b\u5176\u4e2d\u7684\u811a\u672c\u662f\u5426\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff0c\u5148\u7528echo\u4ee3\u66ff\u4e86\u771f\u5b9e\u7684iptables\u5c4f\u853d\uff0c\u4f46\u662f\u5c31\u662f\u65e0\u6cd5\u6309\u7167\u9884\u60f3\u7684\u6b63\u5e38\u5de5\u4f5c\uff1a<\/div>\n<div>\n<pre class=\"lang:default decode:true \">awk '\/ 404 \/{status404[$1]++}; END {for (IP in status404) print status404[IP]\"t\", IP}' $log_file | sort -nr | awk '{if($1&gt;$error_block) {print $2}}' | tee 404_ipList.txt\u00a0\u00a0\u00a0\u00a0# $error_block \u56e0\u4e3a\u662f\u5916\u90e8\u53d8\u91cf\uff0c\u5fc5\u987b\u8981\u7528\u5355\u5f15\u53f7\u5f15\u8d77\u6765\u624d\u884c\uff01\uff01\uff01\n\nwhitelist=$(last | grep -Ev '^$|mosh|reboot|wtmp' | awk '{print $3}' | sort | uniq | xargs)while read ip\ndo\n\tif ! $(echo ${whitelist} | grep -wq ${ip}); then\n\t\techo \"blocking ${ip}\"\n\t\t#$IPTABLES -I INPUT -s ${ip}\/32 -p tcp -m tcp --dport 80 -j DROP\n\tfi\ndone &lt; 404_ipList.txt;<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<pre class=\"lang:default decode:true best-text mb-10\"># awk\u8c03\u7528\u5916\u90e8\u53d8\u91cf\u662f\u9700\u8981\u7528\u201c\u5355\u5f15\u53f7\u201d\u62ec\u8d77\u6765\u3002\u6216\u8005\u5728\u524d\u9762\u7528 -v \u9009\u9879\u6dfb\u52a0\u8fd9\u4e2a\u5916\u90e8\u53d8\u91cf\u3002\nawk '{if($1&gt;='$n') print $2}' file<\/pre>\n<\/div>\n<h6><span style=\"color: #000000; font-size: medium;\">\u53c2\u8003\u7f51\u5740\uff1a<\/span><\/h6>\n<div>\n<ul>\n<li>awk if \u5927\u4e8e shell\u53d8\u91cf_\u767e\u5ea6\u641c\u7d22 <a href=\"http:\/\/www.baidu.com\/s?word=awk+if+%E5%A4%A7%E4%BA%8E+shell%E5%8F%98%E9%87%8F\" target=\"_blank\">http:\/\/www.baidu.com\/s?word=awk+if+%E5%A4%A7%E4%BA%8E+shell%E5%8F%98%E9%87%8F<\/a><\/li>\n<li><span style=\"color: #ff0000;\"><strong>\u5173\u4e8eawk\u4e2dif\u6bd4\u8f83\u8fd0\u7b97\u4f7f\u7528\u53d8\u91cf<\/strong><\/span>-Shell-ChinaUnix.net <a href=\"http:\/\/bbs.chinaunix.net\/thread-4067950-1-1.html\" target=\"_blank\">http:\/\/bbs.chinaunix.net\/thread-4067950-1-1.html<\/a><\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/forum.php?mod=viewthread&amp;tid=4084781&amp;highlight\" target=\"_blank\">http:\/\/bbs.chinaunix.net\/forum.php?mod=viewthread&amp;tid=4084781&amp;highlight<\/a>=<\/li>\n<li>awk\u547d\u4ee4\u8be6\u89e3<a href=\"http:\/\/www.cnblogs.com\/serendipity\/archive\/2011\/08\/01\/2124118.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/serendipity\/archive\/2011\/08\/01\/2124118.html<\/a><\/li>\n<li>Awk\u5b66\u4e60\u7b14\u8bb0 <a href=\"http:\/\/man.lupaworld.com\/content\/manage\/ringkee\/awk.htm\" target=\"_blank\">http:\/\/man.lupaworld.com\/content\/manage\/ringkee\/awk.htm<\/a><\/li>\n<\/ul>\n<\/div>\n<hr \/>\n<h5>\u4e0b\u9762\u662f\u4e00\u4e9bawk\u7684\u5b9e\u9645\u4f8b\u5b50\uff0c\u53ef\u4ee5\u5b66\u4e60\u5b66\u4e60\uff1a<\/h5>\n<h6># {\u6279\u91cf\u91cd\u547d\u540d}<\/h6>\n<pre class=\"lang:default decode:true \">ls junk* | awk '{print \"mv \"$0\" ..\/iraf\/\"$0\".dat\"}' | bash<\/pre>\n<h6>awk\u4e2d\u7684\u4e00\u4e9b\u5185\u7f6e\u53d8\u91cf\/\u51fd\u6570\uff1a<\/h6>\n<pre class=\"lang:default decode:true \"># NF (number of columns){\u5217\u7684\u6570\u91cf}\n# NR (the current line that awk is working on)}{\u6b63\u5728\u5904\u7406\u7684\u5f53\u524d\u884c\u53f7}\n# END (true if awk reaches the EOF)\n# BEGIN (true before awk reads anything)\n# length (number of characters in a line or a string)<\/pre>\n<p>&nbsp;<\/p>\n<p># Print lines longer than 72 characters{\u6253\u5370\u957f\u5ea6\u8d85\u8fc772\u4e2a\u5b57\u7b26\u7684\u884c}<\/p>\n<pre class=\"lang:default decode:true \">awk 'length &gt; 72' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print length of string in 2nd column{\u6253\u5370\u7b2c\u4e8c\u5217\u7684\u5b57\u7b26\u4e32\u957f\u5ea6}<br \/>\nawk &#8216;{print length($2)}&#8217; file<\/p>\n<p># Add up first column, print sum and average{\u5bf9\u7b2c1\u5217\u505a\u7d2f\u52a0\u7136\u540e\u6253\u5370\u603b\u548c+\u5e73\u5747\u503c}<\/p>\n<pre class=\"lang:default decode:true \">{ s += $1 } END{ print \"sum is\", s, \" average is\", s\/NR }<\/pre>\n<p>&nbsp;<\/p>\n<p># Print fields in reverse order{\u5c06\u6bcf\u884c\u5185\u5bb9\u9006\u5e8f\u8f93\u51fa}<\/p>\n<pre class=\"lang:default decode:true \">awk '{ for (i = NF; i &gt; 0; --i) print $i }' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print the last line{<span style=\"color: #ff0000;\"><strong>\u6253\u5370\u6700\u540e\u4e00\u884c<\/strong><\/span>}<\/p>\n<pre class=\"lang:default decode:true \">{line = $0} END{print line}<\/pre>\n<p>&nbsp;<\/p>\n<p># {\u6253\u5370\u884c\u4e2d\u5305\u542b\u6709Pat\u7684\u603b\u884c\u6570}<\/p>\n<pre class=\"lang:default decode:true \">\/Pat\/ {nlines = nlines + 1} END{print nlines}<\/pre>\n<p>&nbsp;<\/p>\n<p># Print all lines between start\/stop pairs{<span style=\"color: #ff0000;\"><strong>\u6253\u5370\u5728start\u548cstop\u4e4b\u95f4\u7684\u6240\u6709\u884c<\/strong><\/span>}<\/p>\n<pre class=\"lang:default decode:true \">awk '\/start\/, \/stop\/' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print column 3 if column 1 &gt; column 2{<span style=\"color: #ff0000;\">\u5982\u679c\u7b2c1\u5217\u7684\u503c\u5927\u4e8e\u7b2c2\u5217\uff0c\u5219\u6253\u5370\u7b2c3\u5217<\/span>}<\/p>\n<pre class=\"lang:default decode:true \">awk '$1 &gt; $2 {print $3}' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print line if column 3 &gt; column 2{\u6253\u5370\u7b2c3\u5217\u5927\u4e8e\u7b2c2\u5217\u7684\u884c}<br \/>\nawk &#8216;$3 &gt; $2&#8217; file<\/p>\n<p># Count number of lines where col 3 &gt; col 1 {<span style=\"color: #ff0000;\"><strong>\u6253\u5370\u7b2c3\u5217\u5927\u4e8e\u7b2c1\u5217\u7684\u603b\u884c\u6570<\/strong><\/span>}<\/p>\n<pre class=\"lang:default decode:true \">awk '$3 &gt; $1 {print i + \"1\"; i++}' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print sequence number and then column 1 of file{<span style=\"color: #ff0000;\">\u6253\u5370\u5e8f\u53f7\u4ee5\u53ca\u6bcf\u884c\u7684\u7b2c1\u5217<\/span>}<\/p>\n<pre class=\"lang:default decode:true \">awk '{print NR, $1}' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print every line after erasing the 2nd field{<span style=\"color: #ff0000;\"><strong>\u4e0d\u6253\u5370\u7b2c2\u5217<\/strong><\/span>}<\/p>\n<pre class=\"lang:default decode:true \">awk '{$2 = \"\"; print}' file<\/pre>\n<p>&nbsp;<\/p>\n<p># Print out 4 random numbers between 0 and 1{\u6253\u53704\u4e2a\u503c\u57280\u30011\u4e4b\u95f4\u7684\u968f\u673a\u6570}<br \/>\nyes | head -4 | awk &#8216;{print rand()}&#8217;<\/p>\n<p># Print out 4 random integers modulo 5{\u6253\u53704\u4e2a\u57280\u548c5\u4e4b\u95f4\u7684\u968f\u673a\u6570}<\/p>\n<pre class=\"lang:default decode:true\">yes | head -4 | awk '{print int(100*rand()) % 5}'<\/pre>\n<p># Replace every field by its absolute value{\u8f93\u51fa\u6bcf\u4e00\u5217\u7684\u7edd\u5bf9\u503c}<br \/>\n{ for (i = 1; i &lt;= NF; i=i+1) if ($i &lt; 0) $i = -$i print}<\/p>\n<p># Print frequency histogram of column of numbers{\u6839\u636e\u7b2c2\u5217\u961f \u503c\u6253\u5370\u7edf\u8ba1\u6570\u636e}<br \/>\n$2 &lt;= 0.1 {na=na+1}<br \/>\n($2 &gt; 0.1) &amp;&amp; ($2 &lt;= 0.2) {nb = nb+1}<br \/>\n($2 &gt; 0.2) &amp;&amp; ($2 &lt;= 0.3) {nc = nc+1}<br \/>\n($2 &gt; 0.3) &amp;&amp; ($2 &lt;= 0.4) {nd = nd+1}<br \/>\n($2 &gt; 0.4) &amp;&amp; ($2 &lt;= 0.5) {ne = ne+1}<br \/>\n($2 &gt; 0.5) &amp;&amp; ($2 &lt;= 0.6) {nf = nf+1}<br \/>\n($2 &gt; 0.6) &amp;&amp; ($2 &lt;= 0.7) {ng = ng+1}<br \/>\n($2 &gt; 0.7) &amp;&amp; ($2 &lt;= 0.8) {nh = nh+1}<br \/>\n($2 &gt; 0.8) &amp;&amp; ($2 &lt;= 0.9) {ni = ni+1}<br \/>\n($2 &gt; 0.9) {nj = nj+1}<br \/>\nEND {print na, nb, nc, nd, ne, nf, ng, nh, ni, nj, NR}<\/p>\n<p># Find maximum and minimum values present in column 1{<span style=\"color: #ff0000;\"><strong>\u627e\u51fa\u7b2c1\u5217\u4e2d\u7684\u6700\u503c\uff0c\u8bb0\u5f97\u5f3a\u5236\u8f6c\u6362\u7c7b\u578b\uff01<\/strong><\/span>}<\/p>\n<pre class=\"lang:default decode:true \">NR == 1 {m=$1 ; p=$1}\n$1+0 &gt;= m+0 {m = $1}\n$1+0 &lt;= p+0 {p = $1}\nEND{ print \"Max = \" m, \" Min = \" p }<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u7f51\u5740\uff1a<a href=\"http:\/\/sparky.rice.edu\/~hartigan\/awk.html\" target=\"_blank\">http:\/\/sparky.rice.edu\/~hartigan\/awk.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u51c6\u5907\u5b9e\u9645\u6d4b\u8bd5\u4e00\u4e0b\u6628\u5929\u4fee\u6539\u8fc7\u7684\u201c\u81ea\u52a8\u5c4f\u853d\u4e00\u4e9b\u6076\u610fIP\u201d\u7684\u6587\u7ae0\uff0c\u770b\u770b\u5176\u4e2d\u7684\u811a\u672c\u662f\u5426\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff0c\u5148\u7528echo\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[74],"class_list":["post-641","post","type-post","status-publish","format-standard","hentry","category-tools","tag-awk"],"views":2681,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/641","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=641"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/641\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}