{"id":195,"date":"2017-02-09T11:08:46","date_gmt":"2017-02-09T03:08:46","guid":{"rendered":"http:\/\/www.thinkmesh.net\/?p=195"},"modified":"2017-02-09T14:04:57","modified_gmt":"2017-02-09T06:04:57","slug":"%e7%ae%80%e6%98%8eawk%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.thinkmesh.net\/?p=195","title":{"rendered":"\u7b80\u660eAWK\u6559\u7a0b"},"content":{"rendered":"<div class=\"alert alert-success\" role=\"alert\">\u672c\u6587\u5185\u5bb9\u6574\u7406\u81ea\u7f51\u7edc\uff0c\u6700\u65b0\u5185\u5bb9\u8bf7\u67e5\u770b<a href=\"http:\/\/coolshell.cn\/articles\/9070.html\">\u539f\u4f5c\u8005\u7f51\u7ad9<\/a>\u3002<\/div>\n<p><a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a>\u540d\u5b57\u53d6\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho\uff0cPeter Weinberger, \u548c Brian Kernighan \u7684Family Name\u7684\u9996\u5b57\u7b26\u3002\u82e5\u8981\u6df1\u5165\u7684\u5b66\u4e60<a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a>\uff0c\u8bf7\u770b\u201cThe <a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a> Programming Language\u201d\u3002\u672c\u6559\u7a0b\u5e76\u4e0d\u9762\u9762\u4ff1\u5230\uff0c\u5168\u6587\u4ee5\u793a\u4f8b\u4e3a\u4e3b\u3002<\/p>\n<h2>\u8d77\u6b65\u4e0a\u53f0<\/h2>\n<p>\u4ecenetstat\u547d\u4ee4\u4e2d\u63d0\u53d6\u5982\u4e0b\u4fe1\u606f\u4f5c\u4e3a\u7528\u4f8b:<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ cat netstat.txt\r\nProto Recv-Q Send-Q Local-Address          Foreign-Address             State\r\ntcp        0      0 0.0.0.0:3306           0.0.0.0:*                   LISTEN\r\ntcp        0      0 0.0.0.0:80             0.0.0.0:*                   LISTEN\r\ntcp        0      0 127.0.0.1:9000         0.0.0.0:*                   LISTEN\r\ntcp        0      0 coolshell.cn:80        124.205.5.146:18245         TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        61.140.101.185:37538        FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:1032        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49809       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        116.234.127.77:11502        FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49829       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        183.60.215.36:36970         TIME_WAIT\r\ntcp        0   4166 coolshell.cn:80        61.148.242.38:30901         ESTABLISHED\r\ntcp        0      1 coolshell.cn:80        124.152.181.209:26825       FIN_WAIT1\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:4796        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        183.60.212.163:51082        TIME_WAIT\r\ntcp        0      1 coolshell.cn:80        208.115.113.92:50601        LAST_ACK\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49840       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        117.136.20.85:50025         FIN_WAIT2\r\ntcp        0      0 :::22                  :::*                        LISTEN\r\n<\/pre>\n<p>\u4e0b\u9762\u4e3a\u6700\u7b80\u5355\u4e14\u6700\u5e38\u7528\u7684<a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a>\u793a\u4f8b\uff0c\u5176\u8f93\u51fa\u7b2c1\u5217\u548c\u7b2c4\u4f8b\u3002<\/p>\n<ul>\n<li>\u5355\u5f15\u53f7\u4e2d\u7684\u88ab\u5927\u62ec\u53f7\u5305\u56f4\u7684\u662f<a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a>\u8bed\u53e5(\u6ce8\u610f\uff0c\u5176\u53ea\u80fd\u88ab\u5355\u5f15\u53f7\u5305\u542b)\uff1b<\/li>\n<li>$1..$n\u8868\u793a\u7b2c\u51e0\u4f8b(\u6ce8\uff1a$0\u8868\u793a\u6574\u4e2a\u884c)\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;{print $1, $4}&#039; netstat.txt\r\nProto Local-Address\r\ntcp 0.0.0.0:3306\r\ntcp 0.0.0.0:80\r\ntcp 127.0.0.1:9000\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp coolshell.cn:80\r\ntcp :::22\r\n<\/pre>\n<p>\u518d\u6765\u770b<a href=\"http:\/\/www.thinkmesh.net\/?tag=awk\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8eAWK\u7684\u6587\u7ae0\" target=\"_blank\">AWK<\/a>\u7684\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u548cC\u8bed\u8a00\u7684printf\u7c7b\u4f3c\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;{printf &quot;%-8s %-8s %-8s %-18s %-22s %-15s\\n&quot;,$1,$2,$3,$4,$5,$6}&#039; netstat.txt\r\nProto    Recv-Q   Send-Q   Local-Address      Foreign-Address        State\r\ntcp      0        0        0.0.0.0:3306       0.0.0.0:*              LISTEN\r\ntcp      0        0        0.0.0.0:80         0.0.0.0:*              LISTEN\r\ntcp      0        0        127.0.0.1:9000     0.0.0.0:*              LISTEN\r\ntcp      0        0        coolshell.cn:80    124.205.5.146:18245    TIME_WAIT\r\ntcp      0        0        coolshell.cn:80    61.140.101.185:37538   FIN_WAIT2\r\ntcp      0        0        coolshell.cn:80    110.194.134.189:1032   ESTABLISHED\r\ntcp      0        0        coolshell.cn:80    123.169.124.111:49809  ESTABLISHED\r\ntcp      0        0        coolshell.cn:80    116.234.127.77:11502   FIN_WAIT2\r\ntcp      0        0        coolshell.cn:80    123.169.124.111:49829  ESTABLISHED\r\ntcp      0        0        coolshell.cn:80    183.60.215.36:36970    TIME_WAIT\r\ntcp      0        4166     coolshell.cn:80    61.148.242.38:30901    ESTABLISHED\r\ntcp      0        1        coolshell.cn:80    124.152.181.209:26825  FIN_WAIT1\r\ntcp      0        0        coolshell.cn:80    110.194.134.189:4796   ESTABLISHED\r\ntcp      0        0        coolshell.cn:80    183.60.212.163:51082   TIME_WAIT\r\ntcp      0        1        coolshell.cn:80    208.115.113.92:50601   LAST_ACK\r\ntcp      0        0        coolshell.cn:80    123.169.124.111:49840  ESTABLISHED\r\ntcp      0        0        coolshell.cn:80    117.136.20.85:50025    FIN_WAIT2\r\ntcp      0        0        :::22              :::*                   LISTEN\r\n<\/pre>\n<h2>\u8131\u6389\u5916\u5957<\/h2>\n<p>\u518d\u6765\u770b\u5982\u4f55\u8fc7\u6ee4\u8bb0\u5f55(\u4e0b\u9762\u8fc7\u6ee4\u6761\u4ef6\u4e3a\uff1a\u7b2c\u4e09\u5217\u7684\u503c\u4e3a0 &#038;&#038; \u7b2c6\u5217\u7684\u503c\u4e3aLISTEN)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$3==0 &amp;&amp; $6==&quot;LISTEN&quot; &#039; netstat.txt\r\ntcp        0      0 0.0.0.0:3306               0.0.0.0:*              LISTEN\r\ntcp        0      0 0.0.0.0:80                 0.0.0.0:*              LISTEN\r\ntcp        0      0 127.0.0.1:9000             0.0.0.0:*              LISTEN\r\ntcp        0      0 :::22                      :::*                   LISTEN\r\n<\/pre>\n<p>\u5176\u4e2d\uff0c\u201c==\u201d\u4e3a\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u5176\u4ed6\u7684\u8fd8\u6709: !=, >, <, >=, <=\u3002\u6765\u770b\u770b\u5404\u79cd\u8fc7\u6ee4\u8bb0\u5f55\u7684\u65b9\u5f0f\uff1a\n\n\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039; $3&gt;0 {print $0}&#039; netstat.txt\r\nProto Recv-Q Send-Q Local-Address          Foreign-Address             State\r\ntcp        0   4166 coolshell.cn:80        61.148.242.38:30901         ESTABLISHED\r\ntcp        0      1 coolshell.cn:80        124.152.181.209:26825       FIN_WAIT1\r\ntcp        0      1 coolshell.cn:80        208.115.113.92:50601        LAST_ACK\r\n<\/pre>\n<p>\u82e5\u9700\u8868\u5934\uff0c\u53ef\u4ee5\u5f15\u5165\u5185\u5efa\u53d8\u91cfNR\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$3==0 &amp;&amp; $6==&quot;LISTEN&quot; || NR==1 &#039; netstat.txt\r\nProto Recv-Q Send-Q Local-Address          Foreign-Address             State\r\ntcp        0      0 0.0.0.0:3306           0.0.0.0:*                   LISTEN\r\ntcp        0      0 0.0.0.0:80             0.0.0.0:*                   LISTEN\r\ntcp        0      0 127.0.0.1:9000         0.0.0.0:*                   LISTEN\r\ntcp        0      0 :::22                  :::*                        LISTEN\r\n<\/pre>\n<p>\u52a0\u4e0a\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$3==0 &amp;&amp; $6==&quot;LISTEN&quot; || NR==1 {printf &quot;%-20s %-20s %s\\n&quot;,$4,$5,$6}&#039; netstat.txt\r\nLocal-Address        Foreign-Address      State\r\n0.0.0.0:3306         0.0.0.0:*            LISTEN\r\n0.0.0.0:80           0.0.0.0:*            LISTEN\r\n127.0.0.1:9000       0.0.0.0:*            LISTEN\r\n:::22                :::*                 LISTEN\r\n<\/pre>\n<p>\u8c08\u5230\u5185\u5efa\u53d8\u91cf\uff0c\u4e0b\u9762\u5217\u51faAWK\u7684\u4e00\u4e9b\u5185\u5efa\u53d8\u91cf\uff0c\u4ee5\u4f9b\u53c2\u8003\uff1a<br \/>\n<div id=\"sc_tips\"><br \/>\n$0\t       \u5f53\u524d\u8bb0\u5f55\uff08\u8fd9\u4e2a\u53d8\u91cf\u4e2d\u5b58\u653e\u7740\u6574\u4e2a\u884c\u7684\u5185\u5bb9\uff09<br \/>\n$1~$n\t\u5f53\u524d\u8bb0\u5f55\u7684\u7b2cn\u4e2a\u5b57\u6bb5\uff0c\u5b57\u6bb5\u95f4\u7531FS\u5206\u9694<br \/>\nFS\t       \u8f93\u5165\u5b57\u6bb5\u5206\u9694\u7b26 \u9ed8\u8ba4\u662f\u7a7a\u683c\u6216Tab<br \/>\nNF\t       \u5f53\u524d\u8bb0\u5f55\u4e2d\u7684\u5b57\u6bb5\u4e2a\u6570\uff0c\u5c31\u662f\u6709\u591a\u5c11\u5217<br \/>\nNR\t       \u5df2\u7ecf\u8bfb\u51fa\u7684\u8bb0\u5f55\u6570\uff0c\u5c31\u662f\u884c\u53f7\uff0c\u4ece1\u5f00\u59cb\uff0c\u5982\u679c\u6709\u591a\u4e2a\u6587\u4ef6\u8bdd\uff0c\u8fd9\u4e2a\u503c\u4e5f\u662f\u4e0d\u65ad\u7d2f\u52a0\u4e2d\u3002<br \/>\nFNR\t\u5f53\u524d\u8bb0\u5f55\u6570\uff0c\u4e0eNR\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u4e2a\u503c\u4f1a\u662f\u5404\u4e2a\u6587\u4ef6\u81ea\u5df1\u7684\u884c\u53f7<br \/>\nRS\t       \u8f93\u5165\u7684\u8bb0\u5f55\u5206\u9694\u7b26\uff0c \u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26<br \/>\nOFS\t\u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26\uff0c \u9ed8\u8ba4\u4e5f\u662f\u7a7a\u683c<br \/>\nORS\t\u8f93\u51fa\u7684\u8bb0\u5f55\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26<br \/>\nFILENAME\t\u5f53\u524d\u8f93\u5165\u6587\u4ef6\u7684\u540d\u5b57<\/div><br \/>\n\u5bf9\u4e8e\u4e0a\u9762\u7684\u5185\u5efa\u53d8\u91cf\uff0c\u5177\u4f53\u5982\u4f55\u4f7f\u7528\u5462\uff1f\u4f8b\u5982\uff0c\u8f93\u51fa\u884c\u53f7\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$3==0 &amp;&amp; $6==&quot;ESTABLISHED&quot; || NR==1 {printf &quot;%02s %s %-20s %-20s %s\\n&quot;,NR, FNR, $4,$5,$6}&#039; netstat.txt\r\n01 1 Local-Address        Foreign-Address      State\r\n07 7 coolshell.cn:80      110.194.134.189:1032 ESTABLISHED\r\n08 8 coolshell.cn:80      123.169.124.111:49809 ESTABLISHED\r\n10 10 coolshell.cn:80      123.169.124.111:49829 ESTABLISHED\r\n14 14 coolshell.cn:80      110.194.134.189:4796 ESTABLISHED\r\n17 17 coolshell.cn:80      123.169.124.111:49840 ESTABLISHED\r\n<\/pre>\n<p>\u4f8b\u5982\uff0c\u6307\u5b9a\u5206\u9694\u7b26<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$  awk  &#039;BEGIN{FS=&quot;:&quot;} {print $1,$3,$6}&#039; \/etc\/passwd\r\nroot 0 \/root\r\nbin 1 \/bin\r\ndaemon 2 \/sbin\r\nadm 3 \/var\/adm\r\nlp 4 \/var\/spool\/lpd\r\nsync 5 \/sbin\r\nshutdown 6 \/sbin\r\nhalt 7 \/sbin\r\n<\/pre>\n<p>\u4e0a\u9762\u7b49\u4ef7\u4e8e(-F\u7684\u610f\u601d\u5c31\u662f\u6307\u5b9a\u5206\u9694\u7b26)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk  -F: &#039;{print $1,$3,$6}&#039; \/etc\/passwd\r\n<\/pre>\n<p>\u6ce8\uff1a\u5982\u679c\u4f60\u8981\u6307\u5b9a\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nawk -F &#039;[;:]&#039;\r\n<\/pre>\n<p>\u518d\u6765\u770b\u4e00\u4e2a\u4ee5\\t\u4f5c\u4e3a\u5206\u9694\u7b26\u8f93\u51fa\u7684\u4f8b\u5b50(\u4e0b\u9762\u4f7f\u7528\u4e86\/etc\/passwd\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u4ee5:\u5206\u9694\u7684)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk  -F: &#039;{print $1,$3,$6}&#039; OFS=&quot;\\t&quot; \/etc\/passwd\r\nroot    0       \/root\r\nbin     1       \/bin\r\ndaemon  2       \/sbin\r\nadm     3       \/var\/adm\r\nlp      4       \/var\/spool\/lpd\r\nsync    5       \/sbin\r\n<\/pre>\n<h2>\u8131\u6389\u886c\u886b<\/h2>\n<p>\u518d\u6765\u770b\u51e0\u4e2a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$6 ~ \/FIN\/ || NR==1 {print NR,$4,$5,$6}&#039; OFS=&quot;\\t&quot; netstat.txt\r\n1       Local-Address   Foreign-Address State\r\n6       coolshell.cn:80 61.140.101.185:37538    FIN_WAIT2\r\n9       coolshell.cn:80 116.234.127.77:11502    FIN_WAIT2\r\n13      coolshell.cn:80 124.152.181.209:26825   FIN_WAIT1\r\n18      coolshell.cn:80 117.136.20.85:50025     FIN_WAIT2\r\n \r\n$ awk &#039;$6 ~ \/WAIT\/ || NR==1 {print NR,$4,$5,$6}&#039; OFS=&quot;\\t&quot; netstat.txt\r\n1       Local-Address   Foreign-Address State\r\n5       coolshell.cn:80 124.205.5.146:18245     TIME_WAIT\r\n6       coolshell.cn:80 61.140.101.185:37538    FIN_WAIT2\r\n9       coolshell.cn:80 116.234.127.77:11502    FIN_WAIT2\r\n11      coolshell.cn:80 183.60.215.36:36970     TIME_WAIT\r\n13      coolshell.cn:80 124.152.181.209:26825   FIN_WAIT1\r\n15      coolshell.cn:80 183.60.212.163:51082    TIME_WAIT\r\n18      coolshell.cn:80 117.136.20.85:50025     FIN_WAIT2\r\n<\/pre>\n<p>\u4e0a\u8ff0\u7b2c1\u4e2a\u4f8b\u5b50\u5339\u914dFIN\u72b6\u6001\uff0c \u7b2c2\u4e2a\u4f8b\u5b50\u5339\u914dWAIT\u5b57\u6837\u7684\u72b6\u6001\u3002\u5176\u5b9e ~ \u8868\u793a\u6a21\u5f0f\u5f00\u59cb\u3002\/ \/\u4e2d\u662f\u6a21\u5f0f\u3002\u8fd9\u5c31\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u3002\u5176\u5b9eawk\u53ef\u4ee5\u50cfgrep\u4e00\u6837\u7684\u53bb\u5339\u914d\u7b2c\u4e00\u884c\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;\/LISTEN\/&#039; netstat.txt\r\ntcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN\r\ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN\r\ntcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN\r\ntcp        0      0 :::22                   :::*                    LISTEN\r\n<\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528 \u201c\/FIN|TIME\/\u201d \u6765\u5339\u914d FIN \u6216\u8005 TIME :<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$6 ~ \/FIN|TIME\/ || NR==1 {print NR,$4,$5,$6}&#039; OFS=&quot;\\t&quot; netstat.txt\r\n1       Local-Address   Foreign-Address State\r\n5       coolshell.cn:80 124.205.5.146:18245     TIME_WAIT\r\n6       coolshell.cn:80 61.140.101.185:37538    FIN_WAIT2\r\n9       coolshell.cn:80 116.234.127.77:11502    FIN_WAIT2\r\n11      coolshell.cn:80 183.60.215.36:36970     TIME_WAIT\r\n13      coolshell.cn:80 124.152.181.209:26825   FIN_WAIT1\r\n15      coolshell.cn:80 183.60.212.163:51082    TIME_WAIT\r\n18      coolshell.cn:80 117.136.20.85:50025     FIN_WAIT2\r\n<\/pre>\n<p>\u518d\u6765\u770b\u770b\u6a21\u5f0f\u53d6\u53cd\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;$6 !~ \/WAIT\/ || NR==1 {print NR,$4,$5,$6}&#039; OFS=&quot;\\t&quot; netstat.txt\r\n1       Local-Address   Foreign-Address State\r\n2       0.0.0.0:3306    0.0.0.0:*       LISTEN\r\n3       0.0.0.0:80      0.0.0.0:*       LISTEN\r\n4       127.0.0.1:9000  0.0.0.0:*       LISTEN\r\n7       coolshell.cn:80 110.194.134.189:1032    ESTABLISHED\r\n8       coolshell.cn:80 123.169.124.111:49809   ESTABLISHED\r\n10      coolshell.cn:80 123.169.124.111:49829   ESTABLISHED\r\n12      coolshell.cn:80 61.148.242.38:30901     ESTABLISHED\r\n14      coolshell.cn:80 110.194.134.189:4796    ESTABLISHED\r\n16      coolshell.cn:80 208.115.113.92:50601    LAST_ACK\r\n17      coolshell.cn:80 123.169.124.111:49840   ESTABLISHED\r\n19      :::22   :::*    LISTEN\r\n<\/pre>\n<p>\u6216\u662f\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nawk &#039;!\/WAIT\/&#039; netstat.txt\r\n<\/pre>\n<p><strong>\u62c6\u5206\u6587\u4ef6<\/strong>\uff1aAWK\u62c6\u5206\u6587\u4ef6\u5f88\u7b80\u5355\uff0c\u4f7f\u7528\u91cd\u5b9a\u5411\u5c31\u597d\u4e86\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u662f\u6309\u7b2c6\u4f8b\u5206\u9694\u6587\u4ef6\uff0c\u76f8\u5f53\u7684\u7b80\u5355(\u5176\u4e2d\u7684NR!=1\u8868\u793a\u4e0d\u5904\u7406\u8868\u5934)\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;NR!=1{print &gt; $6}&#039; netstat.txt\r\n \r\n$ ls\r\nESTABLISHED  FIN_WAIT1  FIN_WAIT2  LAST_ACK  LISTEN  netstat.txt  TIME_WAIT\r\n \r\n$ cat ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:1032        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49809       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49829       ESTABLISHED\r\ntcp        0   4166 coolshell.cn:80        61.148.242.38:30901         ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:4796        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49840       ESTABLISHED\r\n \r\n$ cat FIN_WAIT1\r\ntcp        0      1 coolshell.cn:80        124.152.181.209:26825       FIN_WAIT1\r\n \r\n$ cat FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        61.140.101.185:37538        FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        116.234.127.77:11502        FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        117.136.20.85:50025         FIN_WAIT2\r\n\r\n$ cat LAST_ACK\r\ntcp        0      1 coolshell.cn:80        208.115.113.92:50601        LAST_ACK\r\n \r\n$ cat LISTEN\r\ntcp        0      0 0.0.0.0:3306           0.0.0.0:*                   LISTEN\r\ntcp        0      0 0.0.0.0:80             0.0.0.0:*                   LISTEN\r\ntcp        0      0 127.0.0.1:9000         0.0.0.0:*                   LISTEN\r\ntcp        0      0 :::22                  :::*                        LISTEN\r\n \r\n$ cat TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        124.205.5.146:18245         TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        183.60.215.36:36970         TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        183.60.212.163:51082        TIME_WAIT\r\n<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u628a\u6307\u5b9a\u7684\u5217\u8f93\u51fa\u5230\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nawk &#039;NR!=1{print $4,$5 &gt; $6}&#039; netstat.txt\r\n<\/pre>\n<p>\u518d\u590d\u6742\u4e00\u70b9(\u6ce8\u610f\u5176\u4e2d\u7684if-else-if\u8bed\u53e5\uff0c\u53ef\u89c1awk\u5176\u5b9e\u662f\u4e2a\u811a\u672c\u89e3\u91ca\u5668)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;NR!=1{if($6 ~ \/TIME|ESTABLISHED\/) print &gt; &quot;1.txt&quot;;\r\nelse if($6 ~ \/LISTEN\/) print &gt; &quot;2.txt&quot;;\r\nelse print &gt; &quot;3.txt&quot; }&#039; netstat.txt\r\n \r\n$ ls ?.txt\r\n1.txt  2.txt  3.txt\r\n \r\n$ cat 1.txt\r\ntcp        0      0 coolshell.cn:80        124.205.5.146:18245         TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:1032        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49809       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49829       ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        183.60.215.36:36970         TIME_WAIT\r\ntcp        0   4166 coolshell.cn:80        61.148.242.38:30901         ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        110.194.134.189:4796        ESTABLISHED\r\ntcp        0      0 coolshell.cn:80        183.60.212.163:51082        TIME_WAIT\r\ntcp        0      0 coolshell.cn:80        123.169.124.111:49840       ESTABLISHED\r\n \r\n$ cat 2.txt\r\ntcp        0      0 0.0.0.0:3306           0.0.0.0:*                   LISTEN\r\ntcp        0      0 0.0.0.0:80             0.0.0.0:*                   LISTEN\r\ntcp        0      0 127.0.0.1:9000         0.0.0.0:*                   LISTEN\r\ntcp        0      0 :::22                  :::*                        LISTEN\r\n\r\n$ cat 3.txt\r\ntcp        0      0 coolshell.cn:80        61.140.101.185:37538        FIN_WAIT2\r\ntcp        0      0 coolshell.cn:80        116.234.127.77:11502        FIN_WAIT2\r\ntcp        0      1 coolshell.cn:80        124.152.181.209:26825       FIN_WAIT1\r\ntcp        0      1 coolshell.cn:80        208.115.113.92:50601        LAST_ACK\r\ntcp        0      0 coolshell.cn:80        117.136.20.85:50025         FIN_WAIT2\r\n<\/pre>\n<p><strong>\u7edf\u8ba1<\/strong>\uff1a\u4e0b\u9762\u7684\u547d\u4ee4\u8ba1\u7b97\u6240\u6709\u7684C\u6587\u4ef6\uff0cCPP\u6587\u4ef6\u548cH\u6587\u4ef6\u7684\u6587\u4ef6\u5927\u5c0f\u603b\u548c\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ ls -l  *.cpp *.c *.h | awk &#039;{sum+=$5} END {print sum}&#039;\r\n2511401\r\n<\/pre>\n<p>\u518d\u6765\u770b\u4e00\u4e2a\u7edf\u8ba1\u5404\u4e2aconnection\u72b6\u6001\u7684\u7528\u6cd5(\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e9b\u7f16\u7a0b\u7684\u5f71\u5b50\u4e86\uff0c\u5927\u5bb6\u90fd\u662f\u7a0b\u5e8f\u5458\u6211\u5c31\u4e0d\u89e3\u91ca\u4e86\u3002\u6ce8\u610f\u5176\u4e2d\u7684\u6570\u7ec4\u7684\u7528\u6cd5)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk &#039;NR!=1{a[$6]++;} END {for (i in a) print i &quot;, &quot; a[i];}&#039; netstat.txt\r\nTIME_WAIT, 3\r\nFIN_WAIT1, 1\r\nESTABLISHED, 6\r\nFIN_WAIT2, 3\r\nLAST_ACK, 1\r\nLISTEN, 4\r\n<\/pre>\n<p>\u518d\u6765\u770b\u770b\u7edf\u8ba1\u6bcf\u4e2a\u7528\u6237\u7684\u8fdb\u7a0b\u7684\u5360\u4e86\u591a\u5c11\u5185\u5b58(\u6ce8\uff1asum\u7684RSS\u90a3\u4e00\u5217)<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ ps aux | awk &#039;NR!=1{a[$1]+=$6;} END { for(i in a) print i &quot;, &quot; a[i]&quot;KB&quot;;}&#039;\r\ndbus, 540KB\r\nmysql, 99928KB\r\nwww, 3264924KB\r\nroot, 63644KB\r\nhchen, 6020KB\r\n<\/pre>\n<h2>\u8131\u6389\u5185\u8863<\/h2>\n<p><strong>awk\u811a\u672c<\/strong>\uff1a\u5728\u4e0a\u9762\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e2aEND\u5173\u952e\u5b57\u3002END\u7684\u610f\u601d\u662f\u201c\u5904\u7406\u5b8c\u6240\u6709\u7684\u884c\u7684\u6807\u8bc6\u201d\uff0c\u5373\u7136\u8bf4\u5230\u4e86END\u5c31\u6709\u5fc5\u8981\u4ecb\u7ecd\u4e00\u4e0bBEGIN\uff0c\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u610f\u5473\u7740\u6267\u884c\u524d\u548c\u6267\u884c\u540e\u7684\u610f\u601d\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>BEGIN{ \u8fd9\u91cc\u9762\u653e\u7684\u662f\u6267\u884c\u524d\u7684\u8bed\u53e5 }<\/li>\n<li>END {\u8fd9\u91cc\u9762\u653e\u7684\u662f\u5904\u7406\u5b8c\u6240\u6709\u7684\u884c\u540e\u8981\u6267\u884c\u7684\u8bed\u53e5 }<\/li>\n<li>{\u8fd9\u91cc\u9762\u653e\u7684\u662f\u5904\u7406\u6bcf\u4e00\u884c\u65f6\u8981\u6267\u884c\u7684\u8bed\u53e5}<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u8bf4\u6e05\u695a\u8fd9\u4e2a\u4e8b\uff0c\u6211\u4eec\u6765\u770b\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u3002\u5047\u8bbe\u6709\u8fd9\u4e48\u4e00\u4e2a\u6587\u4ef6(\u5b66\u751f\u6210\u7ee9\u8868)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ cat score.txt\r\nMarry   2143 78 84 77\r\nJack    2321 66 78 45\r\nTom     2122 48 77 71\r\nMike    2537 87 97 95\r\nBob     2415 40 57 62\r\n<\/pre>\n<p>awk\u811a\u672c\u5982\u4e0b(\u6211\u6ca1\u6709\u5199\u6709\u547d\u4ee4\u884c\u4e0a\u662f\u56e0\u4e3a\u547d\u4ee4\u884c\u4e0a\u4e0d\u6613\u8bfb\uff0c\u53e6\u5916\u4e5f\u5728\u4ecb\u7ecd\u53e6\u4e00\u79cd\u7528\u6cd5)\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ cat cal.awk\r\n#!\/bin\/awk -f\r\n#\u8fd0\u884c\u524d\r\nBEGIN {\r\n    math = 0\r\n    english = 0\r\n    computer = 0\r\n \r\n    printf &quot;NAME    NO.   MATH  ENGLISH  COMPUTER   TOTAL\\n&quot;\r\n    printf &quot;---------------------------------------------\\n&quot;\r\n}\r\n#\u8fd0\u884c\u4e2d\r\n{\r\n    math+=$3\r\n    english+=$4\r\n    computer+=$5\r\n    printf &quot;%-6s %-6s %4d %8d %8d %8d\\n&quot;, $1, $2, $3,$4,$5, $3+$4+$5\r\n}\r\n#\u8fd0\u884c\u540e\r\nEND {\r\n    printf &quot;---------------------------------------------\\n&quot;\r\n    printf &quot;  TOTAL:%10d %8d %8d \\n&quot;, math, english, computer\r\n    printf &quot;AVERAGE:%10.2f %8.2f %8.2f\\n&quot;, math\/NR, english\/NR, computer\/NR\r\n}\r\n\r\n<\/pre>\n<p>\u6765\u770b\u4e00\u4e0b\u6267\u884c\u7ed3\u679c\uff1a\uff08\u4e5f\u53ef\u4ee5\u8fd9\u6837\u8fd0\u884c .\/cal.awk score.txt\uff09<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ awk -f cal.awk score.txt\r\nNAME    NO.   MATH  ENGLISH  COMPUTER   TOTAL\r\n---------------------------------------------\r\nMarry  2143     78       84       77      239\r\nJack   2321     66       78       45      189\r\nTom    2122     48       77       71      196\r\nMike   2537     87       97       95      279\r\nBob    2415     40       57       62      159\r\n---------------------------------------------\r\n  TOTAL:       319      393      350\r\nAVERAGE:     63.80    78.60    70.00\r\n<\/pre>\n<p><strong>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a\u5373\u7136\u8bf4\u5230\u4e86\u811a\u672c\uff0c\u6211\u4eec\u6765\u770b\u770b\u600e\u4e48\u548c\u73af\u5883\u53d8\u91cf\u4ea4\u4e92\uff1a\uff08\u4f7f\u7528-v\u53c2\u6570\u548cENVIRON\uff0c\u4f7f\u7528ENVIRON\u7684\u73af\u5883\u53d8\u91cf\u9700\u8981export\uff09<\/p>\n<pre class=\"prettyprint linenums\" >\r\n$ x=5\r\n \r\n$ y=10\r\n$ export y\r\n \r\n$ echo $x $y\r\n5 10\r\n \r\n$ awk -v val=$x &#039;{print $1, $2, $3, $4+val, $5+ENVIRON[&quot;y&quot;]}&#039; OFS=&quot;\\t&quot; score.txt\r\nMarry   2143    78      89      87\r\nJack    2321    66      83      55\r\nTom     2122    48      82      81\r\nMike    2537    87      102     105\r\nBob     2415    40      62      72\r\n<\/pre>\n<h2>\u51e0\u4e2a\u82b1\u6d3b<\/h2>\n<p>\u518d\u6765\u770b\u51e0\u4e2a\u5c0f\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\n#\u4ecefile\u6587\u4ef6\u4e2d\u627e\u51fa\u957f\u5ea6\u5927\u4e8e80\u7684\u884c\r\nawk &#039;length&gt;80&#039; file\r\n \r\n#\u6309\u8fde\u63a5\u6570\u67e5\u770b\u5ba2\u6237\u7aefIP\r\nnetstat -ntu | awk &#039;{print $5}&#039; | cut -d: -f1 | sort | uniq -c | sort -nr\r\n \r\n#\u6253\u537099\u4e58\u6cd5\u8868\r\nseq 9 | sed &#039;H;g&#039; | awk -v RS=&#039;&#039; &#039;{for(i=1;i&lt;=NF;i++)printf(&quot;%dx%d=%d%s&quot;, i, NR, i*NR, i==NR?&quot;\\n&quot;:&quot;\\t&quot;)}&#039;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5185\u5bb9\u6574\u7406\u81ea\u7f51\u7edc\uff0c\u6700\u65b0\u5185\u5bb9\u8bf7\u67e5\u770b\u539f\u4f5c\u8005\u7f51\u7ad9\u3002 AWK\u540d\u5b57\u53d6\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho\uff0cPeter We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[28],"_links":{"self":[{"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/posts\/195"}],"collection":[{"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":34,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":229,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/229"}],"wp:attachment":[{"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thinkmesh.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}