{"id":181830,"date":"2020-05-14T09:49:06","date_gmt":"2020-05-14T01:49:06","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=181830"},"modified":"2020-05-17T19:48:15","modified_gmt":"2020-05-17T11:48:15","slug":"awk-cmd-example","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-cmd-example.html","title":{"rendered":"awk\u547d\u4ee4\u4f7f\u7528\u5b9e\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n Awk\u662f\u4e3a\u9ad8\u7ea7\u6587\u672c\u5904\u7406\u800c\u8bbe\u8ba1\u7684\u901a\u7528\u811a\u672c\u8bed\u8a00\u3002\u5b83\u4e3b\u8981\u7528\u4f5c\u62a5\u544a\u548c\u5206\u6790\u5de5\u5177\u3002\u672c\u6587\u4ecb\u7ecdawk\u5728\u547d\u4ee4\u884c\u4e2d\u64cd\u4f5c\u6587\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Awk\u4e00\u6b21\u5bf9\u4e00\u6761\u8bb0\u5f55\u8fdb\u884c\u64cd\u4f5c\uff0c\u76f4\u5230\u5230\u8fbe\u8f93\u5165\u7684\u672b\u5c3e\u3002\u8bb0\u5f55\u7531\u4e00\u4e2a\u79f0\u4e3a\u8bb0\u5f55\u5206\u9694\u7b26\u7684\u5b57\u7b26\u5206\u9694\u3002\u9ed8\u8ba4\u7684\u8bb0\u5f55\u5206\u9694\u7b26\u662f\u6362\u884c\u7b26\uff0c\u8fd9\u610f\u5473\u7740\u6587\u672c\u6570\u636e\u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u662f\u4e00\u4e2a\u8bb0\u5f55\u3002<\/p>\n

\u6bcf\u6761\u8bb0\u5f55\u4e2d\u7684\u5b57\u6bb5\u7531$<\/code>\u540e\u8ddf\u5b57\u6bb5\u53f7\uff08\u4ee51\u5f00\u5934\uff09\u5f15\u7528\u3002\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u7528$1<\/code>\u8868\u793a\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u7528$2<\/code>\u8868\u793a\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u4e5f\u53ef\u4ee5\u7528\u7279\u6b8a\u53d8\u91cf$NF<\/code>\u5f15\u7528\u3002\u6574\u4e2a\u8bb0\u5f55\u53ef\u4ee5\u7528$0<\/code>\u5f15\u7528\u3002<\/p>\n

Awk\u6a21\u5f0f<\/strong><\/div>\n

Awk\u652f\u6301\u4e0d\u540c\u7c7b\u578b\u7684\u6a21\u5f0f\uff0c\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5173\u7cfb\u8868\u8fbe\u5f0f\u3001\u8303\u56f4\u548c\u7279\u6b8a\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/p>\n

\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c teams.txt\u201d\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\n[root@localhost ~]# cat teams.txt\r\n\r\nBucks Milwaukee    60 22 0.732 \r\nRaptors Toronto    58 24 0.707 \r\n76ers Philadelphia 51 31 0.622\r\nCeltics Boston     49 33 0.598\r\nPacers Indiana     48 34 0.585\r\n<\/pre>\n
\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/span><\/div>\n

Awk\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7528\u659c\u7ebf\uff08\/\/\uff09\u5305\u542b\u8d77\u6765\uff0c\u4f8b\u5982\uff1a\u8981\u663e\u793a\u5305\u542b\u201c0.5\u201d\u7684\u6bcf\u6761\u8bb0\u5f55\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

\r\n[root@localhost ~]# awk '\/0.5\/ {print $0}' teams.txt \r\nCeltics Boston     49 33 0.598\r\nPacers Indiana     48 34 0.585\r\n<\/pre>\n

\"\"
\n\u8981\u663e\u793a\u5305\u542b\u201c0.5\u201d\u7684\u6bcf\u6761\u8bb0\u5f55\u7684\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\uff1a<\/p>\n

\r\n[root@localhost ~]# awk '\/0.5\/ {print $1,$2}' teams.txt \r\nCeltics Boston\r\nPacers Indiana\r\n<\/pre>\n

\"\"
\n\u663e\u793a\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u6570\u5b57\u5f00\u5934\u7684\u8bb0\u5f55\uff0c\u5e76\u6253\u5370\u7b2c\u4e00\u5217\uff1a<\/p>\n

\r\n[root@localhost ~]# awk '\/^[0-9]\/ {print $1}' teams.txt \r\n76ers\r\n<\/pre>\n

\"\"<\/p>\n

\u5173\u7cfb\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/span><\/div>\n

\u5173\u7cfb\u8868\u8fbe\u5f0f\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u5339\u914d\u7279\u5b9a\u5b57\u6bb5\u6216\u53d8\u91cf\u7684\u5185\u5bb9\u3002
\n\u5e38\u7528\u7684\u5173\u7cfb\u8fd0\u7b97\u7b26\u6709\uff1a<\/p>\n

    \n
  • ~<\/code>\uff1a\u5305\u542b<\/li>\n
  • !~<\/code>\uff1a\u4e0d\u5305\u542b<\/li>\n
  • ><\/code>\uff1a\u5927\u4e8e<\/li>\n
  • <<\/code>\uff1a\u5c0f\u4e8e<\/li>\n
  • >=<\/code>\uff1a\u5927\u4e8e\u7b49\u4e8e<\/li>\n
  • <=<\/code>\uff1a\u5c0f\u4e8e\u7b49\u4e8e<\/li>\n
  • =<\/code>\uff1a\u7b49\u4e8e<\/li>\n
  • !=<\/code>\uff1a\u4e0d\u7b49\u4e8e<\/li>\n<\/ul>\n

    \u4f7f\u7528~<\/code>\u201c\u5305\u542b\u201d\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u663e\u793a\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u4e2d\u5305\u542b\u201cia\u201d\u5b57\u7b26\u7684\u6bcf\u6761\u8bb0\u5f55\uff1a<\/p>\n

    \r\n[root@localhost ~]# awk '$2 ~ \/ia\/ {print $0}' teams.txt \r\n76ers Philadelphia 51 31 0.622\r\nPacers Indiana     48 34 0.585 \r\n<\/pre>\n

    \"\"
    \n\u82e5\u8981\u5339\u914d\u4e0d\u5305\u542b\u7ed9\u5b9a\u5b57\u7b26\u7684\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528!~<\/code>\u4e0d\u5305\u542b\u64cd\u4f5c\u7b26\u3002\u4f8b\u5982\uff0c\u663e\u793a\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u4e2d\u4e0d\u5305\u542b\u201cia\u201d\u5b57\u7b26\u7684\u6bcf\u6761\u8bb0\u5f55\uff1a<\/p>\n

    \r\n[root@localhost ~]# awk '$2 !~ \/ia\/ {print $0}' teams.txt \r\nBucks Milwaukee    60 22 0.732 \r\nRaptors Toronto    58 24 0.707 \r\nCeltics Boston     49 33 0.598 \r\n<\/pre>\n

    \u4f7f\u7528!=<\/code>\u201c\u4e0d\u7b49\u4e8e\u201d\u64cd\u4f5c\u7b26\uff0c
    \n\"\"
    \n\u4f7f\u7528>=<\/code>\u201c\u5927\u4e8e\u7b49\u4e8e\u201d\u64cd\u4f5c\u7b26\uff0c\u663e\u793a\u7b2c\u56db\u4e2a\u5b57\u6bb5\u5927\u4e8e\u7b49\u4e8e30\u7684\u8bb0\u5f55\uff1a<\/p>\n

    \r\n[root@localhost ~]# awk '$4 >= 30 {print $0}' teams.txt \r\n76ers Philadelphia 51 31 0.622\r\nCeltics Boston     49 33 0.598\r\nPacers Indiana     48 34 0.585\r\n<\/pre>\n

    \"\"<\/p>\n

    \u8303\u56f4\u6a21\u5f0f<\/strong><\/span><\/div>\n

    \u8303\u56f4\u6a21\u5f0f\u7531\u9017\u53f7\u5206\u9694\u7684\u4e24\u7ec4\u5b57\u7b26\u7ec4\u6210\uff0c\u4ece\u4e0e\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u8bb0\u5f55\u5f00\u59cb\uff0c\u76f4\u5230\u4e0e\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u7684\u8bb0\u5f55\u5339\u914d\u4e3a\u6b62\u7684\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n

    \u4f8b\u5982\uff0c\u663e\u793a\u4ece\u201cRaptors\u201d\u5230 \u201cCeltics\u201d\u5728\u5185\u7684\u8bb0\u5f55\uff0c\uff1a<\/p>\n

    \r\n[root@localhost ~]# awk '\/Raptors\/,\/Celtics\/ {print $0}' teams.txt \r\nRaptors Toronto    58 24 0.707 \r\n76ers Philadelphia 51 31 0.622\r\nCeltics Boston     49 33 0.598\r\n<\/pre>\n

    \"\"
    \n\u8303\u56f4\u6a21\u5f0f\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5173\u7cfb\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982\uff0c\u663e\u793a\u7b2c\u56db\u4e2a\u5b57\u6bb5\u7b49\u4e8e31\u5230\u7b2c\u56db\u4e2a\u5b57\u6bb5\u7b49\u4e8e34\t\u7684\u8bb0\u5f55\uff1a<\/p>\n

    \r\n[root@localhost ~]# awk '$4 == 31 , $4 == 34 {print $0}' teams.txt \r\n76ers Philadelphia 51 31 0.622\r\nCeltics Boston     49 33 0.598\r\nPacers Indiana     48 34 0.585\r\n<\/pre>\n

    \"\"<\/p>\n

    \u7279\u6b8a\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/span><\/div>\n

    Awk\u5305\u62ec\u4ee5\u4e0b\u7279\u6b8a\u6a21\u5f0f\u3002<\/p>\n

      \n
    • BEGIN<\/code>\uff1a\u5728\u5904\u7406\u8bb0\u5f55\u4e4b\u524d\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/li>\n
    • END<\/code>\uff1a\u7528\u4e8e\u5728\u5904\u7406\u8bb0\u5f55\u4e4b\u540e\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n

      BEGIN\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u8bbe\u7f6e\u53d8\u91cf\uff0cEND\u6a21\u5f0f\u7528\u4e8e\u5904\u7406\u7edf\u8ba1\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u5c06\u663e\u793a\u201cStart Processing.\u201d\uff0c\u7136\u540e\u663e\u793a\u7b2c\u4e09\u4e2a\u5b57\u6bb5\uff0c\u6700\u540e\u663e\u793a\u201dEnd Processing.\u201d\uff1a<\/p>\n

      \r\n[root@localhost ~]# awk 'BEGIN {print \"Start Processing.\"};{print $3};END {print \"End Processing.\"}' teams.txt \r\nStart Processing.\r\n60\r\n58\r\n51\r\n49\r\n48\r\nEnd Processing.\r\n<\/pre>\n

      \"\"<\/p>\n

      \u5185\u7f6e\u53d8\u91cf<\/strong><\/div>\n

      Awk\u6709\u8bb8\u591a\u5185\u7f6e\u53d8\u91cf\uff0c\u5141\u8bb8\u63a7\u5236\u7a0b\u5e8f\u7684\u5904\u7406\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5185\u7f6e\u53d8\u91cf:<\/p>\n

        \n
      • NF<\/code>\uff1a\u8bb0\u5f55\u4e2d\u7684\u5b57\u6bb5\u6570<\/li>\n
      • NR<\/code>\uff1a\u5f53\u524d\u8bb0\u5f55\u7684\u7f16\u53f7<\/li>\n
      • FILENAME<\/code>\uff1a\u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u8f93\u5165\u6587\u4ef6\u540d\u79f0<\/li>\n
      • FS<\/code>\uff1a\u5b57\u6bb5\u5206\u9694\u7b26<\/li>\n
      • RS<\/code>\uff1a\u8bb0\u5f55\u5206\u9694\u7b26<\/li>\n
      • OFS<\/code>\uff1a\u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26<\/li>\n
      • ORS<\/code>\uff1a\u8f93\u51fa\u8bb0\u5f55\u5206\u9694\u7b26<\/li>\n<\/ul>\n

        \u4e0b\u9762\u662f\u4e00\u4e2a\u663e\u793a\u5982\u4f55\u6253\u5370\u6587\u4ef6\u540d\u548c\u884c\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n

        \r\n[root@localhost ~]# awk 'END {print \"File\" , FILENAME , \"contains\" , NR , \"lines.\"}' teams.txt \r\nFile teams.txt contains 5 lines.\r\n<\/pre>\n
        \u603b\u7ed3<\/strong><\/div>\n

        Awk\u662f\u6700\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        Awk\u4e00\u6b21\u5bf9\u4e00\u6761\u8bb0\u5f55\u8fdb\u884c\u64cd\u4f5c\uff0c\u76f4\u5230\u5230\u8fbe\u8f93\u5165\u7684\u672b\u5c3e\u3002\u8bb0\u5f55\u7531\u4e00\u4e2a\u79f0\u4e3a\u8bb0\u5f55\u5206\u9694\u7b26\u7684\u5b57\u7b26\u5206\u9694\u3002\u9ed8\u8ba4\u7684\u8bb0\u5f55\u5206\u9694\u7b26\u662f\u6362\u884c\u7b26 […]<\/p>\n","protected":false},"author":309,"featured_media":181831,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-181830","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\/181830","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=181830"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181830\/revisions"}],"predecessor-version":[{"id":191451,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181830\/revisions\/191451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/181831"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=181830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=181830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=181830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}