{"id":213674,"date":"2021-03-26T08:57:08","date_gmt":"2021-03-26T00:57:08","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=213674"},"modified":"2021-03-16T08:58:18","modified_gmt":"2021-03-16T00:58:18","slug":"awk-in-action","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-in-action.html","title":{"rendered":"Awk \u5b9e\u6218\u8be6\u89e3\u6559\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u77e5\u9053 Linux \u4e09\u5251\u5ba2\uff0c\u5b83\u4eec\u662f grep\u3001sed\u3001awk\u3002\u5728\u524d\u8fb9\u5df2\u7ecf\u8bb2\u8fc7 grep \u548c sed\uff0c\u6ca1\u770b\u8fc7\u7684\u540c\u5b66\u53ef\u4ee5\u76f4\u63a5\u70b9\u51fb\u9605\u8bfb\uff0c\u4eca\u5929\u8981\u5206\u4eab\u7684\u662f\u66f4\u4e3a\u5f3a\u5927\u7684 awk\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6211\u4eec\u77e5\u9053 Linux \u4e09\u5251\u5ba2\uff0c\u5b83\u4eec\u662f grep\u3001sed\u3001awk\u3002\u5728\u524d\u8fb9\u5df2\u7ecf\u8bb2\u8fc7 grep \u548c sed\uff0c\u6ca1\u770b\u8fc7\u7684\u540c\u5b66\u53ef\u4ee5\u76f4\u63a5\u70b9\u51fb\u9605\u8bfb\uff0c\u4eca\u5929\u8981\u5206\u4eab\u7684\u662f\u66f4\u4e3a\u5f3a\u5927\u7684 awk\u3002<\/p>\n

sed \u53ef\u4ee5\u5b9e\u73b0\u975e\u4ea4\u4e92\u5f0f\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff0cgrep \u80fd\u591f\u5b9e\u73b0\u6709\u6548\u7684\u8fc7\u6ee4\u529f\u80fd\u3002\u4e0e\u4e24\u8005\u76f8\u6bd4\uff0cawk \u662f\u4e00\u6b3e\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\uff0c\u5728\u5bf9\u6570\u636e\u5206\u6790\u5e76\u751f\u6210\u62a5\u544a\u65f6\uff0c\u663e\u5f97\u5c24\u4e3a\u5f3a\u608d\u3002<\/p>\n

awk \u5f3a\u5927\u7684\u529f\u80fd\uff0c\u662f\u4e00\u822c Linux \u547d\u4ee4\u65e0\u6cd5\u6bd4\u62df\u7684\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4e0d\u4f1a\u544a\u8bc9\u4f60 awk \u4e5f\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u514d\u5f97\u4f1a\u5413\u5230\u4f60\u3002\u6211\u4eec\u53ea\u9700\u628a\u5b83\u5f53\u505a Linux \u4e0b\u4e00\u6b3e\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u5373\u53ef\u3002<\/p>\n

\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4ecd\u7136\u79c9\u6301\u7740 \u5b9e\u7528\u3001\u5b9e\u8df5 \u539f\u5219\uff0c\u63d0\u4f9b\u5927\u91cf\u7684\u793a\u4f8b\uff0c\u4f46\u4e0d\u4f1a\u9762\u9762\u4ff1\u5230\u3002\u901a\u8fc7\u672c\u6587\u53ef\u4ee5\u5e2e\u52a9\u4f60\uff0c\u5feb\u901f\u5c06 awk \u8fd0\u7528\u8d77\u6765\uff0c\u8fd9\u4e9b\u4e1c\u897f\u8db3\u591f\u5e94\u4ed8\u5de5\u4f5c\u4e2d\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u3002<\/p>\n

\u573a\u666f<\/strong><\/div>\n

\u5b66\u4e60\u5177\u4f53\u4f7f\u7528\u524d\uff0c\u5148\u6765\u770b\u4e0b awk \u80fd\u5e72\u4e9b\u4ec0\u4e48\u4e8b\u60c5\uff1a<\/p>\n

    \n
  1. \u80fd\u591f\u5c06\u7ed9\u5b9a\u7684\u6587\u672c\u5185\u5bb9\uff0c\u6309\u7167\u6211\u4eec\u671f\u671b\u7684\u683c\u5f0f\u8f93\u51fa\u663e\u793a\uff0c\u6253\u5370\u6210\u62a5\u8868\u3002<\/li>\n
  2. \u5206\u6790\u5904\u7406\u7cfb\u7edf\u65e5\u5fd7\uff0c\u5feb\u901f\u5730\u5206\u6790\u6316\u6398\u6211\u4eec\u5173\u5fc3\u7684\u6570\u636e\uff0c\u5e76\u751f\u6210\u7edf\u8ba1\u4fe1\u606f;<\/li>\n
  3. \u65b9\u4fbf\u5730\u7528\u6765\u7edf\u8ba1\u6570\u636e\uff0c\u6bd4\u5982\u7f51\u7ad9\u7684\u8bbf\u95ee\u91cf\uff0c\u8bbf\u95ee\u7684 IP \u91cf\u7b49;<\/li>\n
  4. \u901a\u8fc7\u5404\u79cd\u5de5\u5177\u7684\u7ec4\u5408\uff0c\u5feb\u901f\u5730\u6c47\u603b\u5206\u6790\u7cfb\u7edf\u7684\u8fd0\u884c\u4fe1\u606f\uff0c\u8ba9\u4f60\u5bf9\u7cfb\u7edf\u7684\u8fd0\u884c\u4e86\u5982\u6307\u638c;<\/li>\n
  5. \u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\u8868\u8fbe\u80fd\u529b\uff0c\u652f\u6301\u5faa\u73af\u3001\u6761\u4ef6\u3001\u6570\u7ec4\u7b49\u8bed\u6cd5\uff0c\u52a9\u4f60\u5206\u6790\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e;<\/li>\n
  6. ......<\/li>\n<\/ol>\n

    \u5f53\u7136 awk \u4e0d\u4ec5\u80fd\u505a\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u5f53\u4f60\u5c06\u5b83\u7684\u7528\u6cd5\u878d\u6c47\u8d2f\u901a\u65f6\uff0c\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u7684\u6309\u7167\u4f60\u7684\u610f\u613f\uff0c\u6765\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u3002<\/p>\n

    \u4e0d\u8fc7\u6211\u4eec\u9700\u8981\u77e5\u9053\uff0cawk \u4e0d\u662f\u4e07\u80fd\u7684\uff0c\u5b83\u6bd4\u8f83\u64c5\u957f\u5904\u7406\u683c\u5f0f\u5316\u7684\u6587\u672c\uff0c\u6bd4\u5982 \u65e5\u5fd7\u3001csv \u683c\u5f0f\u6570\u636e\u7b49;<\/p>\n

    \u539f\u7406<\/strong><\/span><\/div>\n

    \u6211\u4eec\u5148\u6765\u7b80\u5355\u4e86\u89e3 awk \u57fa\u672c\u5de5\u4f5c\u539f\u7406\uff0c\u901a\u8fc7\u4e0b\u8fb9\u7684\u56fe\u6587\u8bb2\u8ff0\uff0c\u5e0c\u671b\u4f60\u80fd\u4e86\u89e3 awk \u5230\u5e95\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n

    awk \u57fa\u672c\u547d\u4ee4\u683c\u5f0f<\/strong><\/span><\/div>\n

    \"\"<\/p>\n

    \u7ed3\u5408\u4e0b\u56fe\u6765\u8be6\u7ec6\u8bf4\u660e awk \u5de5\u4f5c\u539f\u7406<\/p>\n

    \"\"<\/p>\n

      \n
    • \u9996\u5148\uff0c\u6267\u884c\u5173\u952e\u5b57 BEGIN \u6807\u8bc6\u7684 {} \u4e2d\u7684\u547d\u4ee4;<\/li>\n
    • \u5b8c\u6210 BEGIN \u5927\u62ec\u53f7\u4e2d\u547d\u4ee4\u7684\u540e\uff0c\u5f00\u59cb\u6267\u884c body \u547d\u4ee4;<\/li>\n
    • \u9010\u884c\u8bfb\u53d6\u6570\u636e\uff0c\u9ed8\u8ba4\u8bfb\u5230 \\n \u5206\u5272\u7684\u5185\u5bb9\u4e3a\u4e00\u6761 \u8bb0\u5f55\uff0c\u5176\u5b9e\u5c31\u662f\u884c\u7684\u6982\u5ff5;<\/li>\n
    • \u5c06\u8bb0\u5f55\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5212\u5206\u4e3a \u5b57\u6bb5\uff0c\u5176\u5b9e\u5c31\u662f\u5217\u7684\u6982\u5ff5;<\/li>\n
    • \u5faa\u73af\u6267\u884c body \u5757\u4e2d\u7684\u547d\u4ee4\uff0c\u6bcf\u8bfb\u53d6\u4e00\u884c\uff0c\u6267\u884c\u4e00\u6b21 body\uff0c\u6700\u7ec8\u5b8c\u6210 body \u6267\u884c;<\/li>\n
    • \u6700\u540e\uff0c\u6267\u884c END \u547d\u4ee4\uff0c\u901a\u5e38\u4f1a\u5728 END \u4e2d\u8f93\u51fa\u6700\u540e\u7684\u7ed3\u679c;<\/li>\n<\/ul>\n

      awk \u662f\u8f93\u5165\u9a71\u52a8\u7684\uff0c\u6709\u591a\u5c11\u8f93\u5165\u884c\uff0c\u5c31\u4f1a\u6267\u884c\u591a\u5c11\u6b21 body \u547d\u4ee4\u3002<\/p>\n

      \u6211\u4eec\u5728\u4e0b\u8fb9\u7684\u793a\u4f8b\u5b66\u4e60\u4e2d\uff0c\u8981\u65f6\u523b\u8bb0\u7740\uff1a\u8bb0\u5f55 (Record) \u5c31\u662f\u884c\uff0c\u5b57\u6bb5 (Field) \u5c31\u662f\u5217\uff0cBEGIN \u662f\u9884\u5904\u7406\u9636\u6bb5\uff0cbody \u662f awk \u771f\u6b63\u5de5\u4f5c\u7684\u9636\u6bb5\uff0cEND \u662f\u6700\u540e\u5904\u7406\u9636\u6bb5\u3002<\/p>\n

      \u5b9e\u6218 - \u5165\u95e8<\/strong><\/span><\/div>\n

      \u4ece\u4e0b\u8fb9\u5185\u5bb9\u5f00\u59cb\uff0c\u6211\u4eec\u76f4\u63a5\u8fdb\u5165\u5230\u5b9e\u6218\u3002\u4e3a\u4e86\u65b9\u4fbf\u4e3e\u4f8b\uff0c\u6211\u5148\u628a\u5982\u4e0b\u4fe1\u606f\u4fdd\u5b58\u5230 file.txt<\/p>\n

      \"\"<\/p>\n

      \u597d\u4e86\uff0c\u6211\u4eec\u5148\u6765\u4e00\u4e2a\u6700\u7b80\u5355\u6700\u5e38\u7528\u7684 awk \u793a\u4f8b\uff0c\u8f93\u51fa\u7b2c 1\u30014\u30018 \u5217\uff1a<\/p>\n

      \"\"<\/p>\n

      \u5927\u62ec\u53f7\u91cc\u8fb9\u7684\u5c31\u662f awk \u8bed\u53e5\uff0c\u53ea\u80fd\u88ab\u5355\u5f15\u53f7\u5305\u542b\uff0c\u5176\u4e2d\uff0c$1..$N\u8868\u793a\u7b2c\u51e0\u5217\uff0c$0 \u8868\u793a\u6574\u4e2a\u884c\u5185\u5bb9<\/p>\n

      \u518d\u6765\u770b\u4e0b awk \u6bd4\u8f83\u5b9e\u7528\u7684\u529f\u80fd \u683c\u5f0f\u5316\u8f93\u51fa\u3002\u548c C \u8bed\u8a00\u7684 printf \u683c\u5f0f\u8f93\u51fa\u662f\u4e00\u6bdb\u4e00\u6837\uff0c\u6211\u4e2a\u4eba\u7279\u522b\u559c\u6b22\u8fd9\u79cd\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u800c\u4e0d\u662f C++ \u4e2d\u7684\u6d41\u7684\u65b9\u5f0f\u3002<\/p>\n

      \"\"<\/p>\n

      %s \u8868\u793a\u5b57\u7b26\u4e32\u5360\u4f4d\u7b26\uff0c-4\u8868\u793a\u5217\u5bbd\u5ea6\u4e3a 4\uff0c\u4e14\u5de6\u5bf9\u9f50\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u5217\u51fa\u66f4\u590d\u6742\u7684\u683c\u5f0f\u6765\uff0c\u8fd9\u91cc\u5148\u4e0d\u8be6\u7ec6\u4e3e\u4f8b\u4e86\u3002<\/p>\n

      \u5b9e\u6218 - \u8fdb\u9636<\/strong><\/span><\/div>\n

      (\u4e00)\u8fc7\u6ee4\u8bb0\u5f55<\/strong><\/p>\n

      \u6709\u4e9b\u6570\u636e\u53ef\u80fd\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8fc7\u6ee4<\/p>\n

      \"\"<\/p>\n

      \u4e0a\u8fb9\u7684\u8fc7\u6ee4\u6761\u4ef6\u4e3a\uff0c\u7b2c 3 \u5217\u4e3a root \u4e14\u7b2c 6 \u5217\u4e3a 10 \u7684\u884c\uff0c\u624d\u4f1a\u88ab\u8f93\u51fa\u3002<\/p>\n

      awk \u652f\u6301\u5404\u79cd\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53f7 !=\u3001>\u3001<\u3001>=\u3001<=\uff0c\u5176\u4e2d $0 \u8868\u793a\u6574\u884c\u7684\u6240\u6709\u5185\u5bb9\u3002\n\n\n(\u4e8c)\u5185\u7f6e\u53d8\u91cf<\/strong><\/p>\n

      awk \u5185\u7f6e\u4e86\u4e00\u4e9b\u53d8\u91cf\uff0c\u66f4\u65b9\u4fbf\u6211\u4eec\u5bf9\u6570\u636e\u7684\u5904\u7406<\/p>\n

      \"\"<\/p>\n

      \u8fc7\u6ee4\u7b2c 3 \u5217\u4e3a root \u7528\u6237\uff0c\u4ee5\u53ca\u7b2c 2 \u884c\u5185\u5bb9\uff0c\u4e14\u6253\u5370\u65f6\u8f93\u51fa\u884c\u53f7\u3002NR \u8868\u793a\u5f53\u524d\u7b2c\u51e0\u884c\uff0cNF\u8868\u793a\u5f53\u524d\u884c\u6709\u51e0\u5217\u3002<\/p>\n

      (\u4e09)\u6307\u5b9a\u5206\u9694\u7b26<\/strong><\/p>\n

      \u6211\u4eec\u7684\u6570\u636e\uff0c\u4e0d\u603b\u662f\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 FS \u53d8\u91cf\u6307\u5b9a\u5206\u9694\u7b26\u3002<\/p>\n

      \"\"<\/p>\n

      \u6211\u4eec\u6307\u5b9a\u5206\u9694\u7b26\u4e3a 2019\uff0c\u8fd9\u6837\u5c31\u5c06\u884c\u5185\u5bb9\u5206\u5272\u4e3a\u4e86\u4e24\u90e8\u5206\uff0c\u5c06 2019 \u66ff\u6362\u6210\u4e86 *<\/p>\n

      \u4e0a\u8fb9\u7684\u547d\u4ee4\u4e5f\u53ef\u4ee5\u901a\u8fc7 -F \u9009\u9879\u6307\u5b9a\u5206\u5272\u7b26<\/p>\n

      \"\"<\/p>\n

      \u5982\u679c\u4f60\u9700\u8981\u6307\u5b9a\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a -F '[;:]'\u3002\u76f8\u4fe1\u806a\u660e\u7684\u4f60\uff0c\u4e00\u5b9a\u80fd\u591f\u7406\u89e3\u5e76\u878d\u4f1a\u8d2f\u901a\u7684\u3002<\/p>\n

      \u540c\u6837\uff0cawk \u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u65f6\u7684\u5206\u9694\u7b26\uff0c\u901a\u8fc7 OFS \u53d8\u91cf\u6765\u8bbe\u7f6e<\/p>\n

      \"\"<\/p>\n

      \u8f93\u51fa\u65f6\uff0c\u5404\u5b57\u6bb5\u7528 OFS \u6307\u5b9a\u7684\u7b26\u53f7\u8fdb\u884c\u4e86\u5206\u9694\u3002<\/p>\n

      \u5b9e\u6218 - \u9ad8\u7ea7<\/strong><\/span><\/div>\n

      (\u4e00)\u6761\u4ef6\u5339\u914d<\/strong><\/p>\n

      \u5217\u51fa root \u7528\u6237\u7684\u6240\u6709\u6587\u4ef6\uff0c\u4ee5\u53ca\u7b2c\u4e00\u884c\u6587\u4ef6<\/p>\n

      \"\"<\/p>\n

      \u4e0a\u8fb9\u5339\u914d\u7b2c\u4e09\u5217\u4e2d\u5305\u542b root \u7684\u884c\uff0c~ \u5176\u5b9e\u5c31\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u3002<\/p>\n

      \u540c\u6837\uff0cawk \u53ef\u4ee5\u50cf grep \u4e00\u6837\u5339\u914d\u67d0\u4e00\u884c\uff0c\u5c31\u50cf\u8fd9\u6837<\/p>\n

      \"\"<\/p>\n

      \u53e6\u5916\uff0c\u53ef\u4ee5\u8fd9\u6837 \/Aug|Dec\/ \u5339\u914d\u591a\u4e2a\u5173\u952e\u8bcd\u3002<\/p>\n

      \u6a21\u5f0f\u53d6\u53cd\u53ef\u4ee5\u4f7f\u7528 ! \u7b26\u53f7<\/p>\n

      \"\"<\/p>\n

      (\u4e8c)\u62c6\u5206\u6587\u4ef6<\/strong><\/p>\n

      \u6211\u4eec\u6765\u505a\u4e00\u4ef6\u6709\u610f\u601d\u7684\u4e8b\u60c5\uff0c\u53ef\u4ee5\u5c06\u6587\u672c\u4fe1\u606f\u62c6\u5206\u4e3a\u591a\u4e2a\u6587\u4ef6\uff0c\u4e0b\u8fb9\u547d\u4ee4\u6309\u7167\u6708\u4efd(\u7b2c5\u5217)\u5c06\u6587\u4ef6\u4fe1\u606f\u62c6\u5206\u4e3a\u591a\u4e2a\u6587\u4ef6<\/p>\n

      \"\"<\/p>\n

      awk \u652f\u6301\u91cd\u5b9a\u5411\u7b26\u53f7 >\uff0c\u76f4\u63a5\u5c06\u6bcf\u884c\u5185\u5bb9\u91cd\u5b9a\u5411\u5230\u6708\u4efd\u547d\u540d\u7684\u6587\u4ef6\u4e86\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u628a\u6307\u5b9a\u7684\u5217\u8f93\u51fa\u5230\u6587\u4ef6<\/p>\n

      (\u4e09)if \u8bed\u53e5<\/strong><\/p>\n

      \u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528 awk \u7684 if \u8bed\u53e5\uff0cawk \u7684\u5f3a\u5927\u6b63\u56e0\u4e3a\u5b83\u662f\u4e2a\u811a\u672c\u89e3\u91ca\u5668\uff0c\u62e5\u6709\u4e00\u822c\u811a\u672c\u8bed\u8a00\u7684\u7f16\u7a0b\u80fd\u529b\uff0c\u4e0b\u8fb9\u793a\u4f8b\u901a\u8fc7\u7a0d\u5fae\u590d\u6742\u7684\u6761\u4ef6\u8fdb\u884c\u62c6\u5206\u6587\u4ef6<\/p>\n

      \"\"<\/p>\n

      \u8981\u6ce8\u610f\uff0cif \u8bed\u53e5\u662f\u5728\u5927\u62ec\u53f7\u91cc\u8fb9\u7684\u3002<\/p>\n

      (\u56db)\u7edf\u8ba1<\/strong><\/p>\n

      \u7edf\u8ba1\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u6240\u6709 *.c\u3001*.h \u6587\u4ef6\u6240\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u603b\u548c<\/p>\n

      \"\"<\/p>\n

      \u7b2c 5 \u5217\u8868\u793a\u6587\u4ef6\u5927\u5c0f\uff0c\u6bcf\u8bfb\u53d6\u4e00\u884c\u5c31\u4f1a\u5c06\u8be5\u6587\u4ef6\u5927\u5c0f\u8ba1\u7b97\u5230 sum \u53d8\u91cf\u4e2d\uff0c\u5728\u6700\u540e END \u9636\u6bb5\u6253\u5370\u51fa sum\uff0c\u4e5f\u5c31\u662f\u6240\u6709\u6587\u4ef6\u7684\u5927\u5c0f\u603b\u548c\u3002<\/p>\n

      \u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u7528\u6237\u7684\u8fdb\u7a0b\u5360\u7528\u4e86\u591a\u5c11\u5185\u5b58\uff0c\u6ce8\u610f\u53d6\u503c\u7684\u662f RSS \u90a3\u4e00\u5217<\/p>\n

      \"\"<\/p>\n

      \u8fd9\u91cc\u7528\u5230\u4e86 \u6570\u7ec4 \u548c for \u5faa\u73af\uff0c\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0cawk \u7684\u6570\u7ec4\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b57\u5178\u6216 Map\uff0ckey \u53ef\u4ee5\u662f\u6570\u503c\u548c\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u5728\u5e73\u65f6\u5f88\u5e38\u7528\u3002<\/p>\n

      (\u4e94)\u5b57\u7b26\u4e32<\/strong><\/p>\n

      \u901a\u8fc7\u4e0b\u8fb9\u7b80\u5355\u793a\u4f8b\uff0c\u5c55\u793a awk \u5bf9\u5b57\u7b26\u4e32\u64cd\u4f5c\u7684\u652f\u6301
      \n\"\"<\/p>\n

      awk \u5185\u7f6e\u652f\u6301\u4e00\u7cfb\u5217\u7684\u5b57\u7b26\u4e32\u51fd\u6570\uff0clength \u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\uff0ctoupper \u51fd\u6570\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u5927\u5199\u3002<\/p>\n

      \u5b9e\u6218 - \u6280\u5de7<\/strong><\/span><\/div>\n

      \u4e3a\u4e86\u4ece\u6574\u4f53\u4e0a\u7406\u89e3 awk \u5de5\u4f5c\u673a\u5236\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e2a\u7efc\u5408\u7684\u793a\u4f8b\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u5355\uff1a<\/p>\n

      \"\"<\/p>\n

      \u7531\u4e8e\u6b64\u793a\u4f8b\u7a0b\u5e8f\u7a0d\u663e\u590d\u6742\uff0c\u5728\u547d\u4ee4\u884c\u4e0a\u4e0d\u6613\u8bfb\uff0c\u53e6\u5916\u5462\uff0c\u4e5f\u60f3\u901a\u8fc7\u6b64\u6848\u4f8b\u4ecb\u7ecd\u53e6\u5916\u4e00\u79cd awk \u7684\u6267\u884c\u65b9\u5f0f\uff0c\u6211\u4eec\u7684 awk \u811a\u672c\u5982\u4e0b\uff1a<\/p>\n

      \"\"<\/p>\n

      \u6267\u884c awk \u7ed3\u679c\u5982\u4e0b<\/p>\n

      \"\"<\/p>\n

      \u6211\u4eec\u53ef\u4ee5\u5c06\u590d\u6742\u7684 awk \u8bed\u53e5\u5199\u5165\u811a\u672c\u6587\u4ef6 cal.awk\uff0c\u7136\u540e\u901a\u8fc7 -f \u9009\u9879\u6307\u5b9a\u4ece\u811a\u672c\u6587\u4ef6\u6267\u884c\u3002<\/p>\n

        \n
      1. \u5728 BEGIN \u9636\u6bb5\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u76f8\u5173\u53d8\u91cf\uff0c\u5e76\u6253\u5370\u4e86\u8868\u5934\u7684\u683c\u5f0f<\/li>\n
      2. \u5728 body \u9636\u6bb5\uff0c\u6211\u4eec\u8bfb\u53d6\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u8ba1\u7b97\u8be5\u5b66\u79d1\u548c\u8be5\u540c\u5b66\u7684\u603b\u6210\u7ee9<\/li>\n
      3. \u5728 END \u9636\u6bb5\uff0c\u6211\u4eec\u5148\u6253\u5370\u4e86\u8868\u5c3e\u7684\u683c\u5f0f\uff0c\u5e76\u6253\u5370\u603b\u6210\u7ee9\uff0c\u4ee5\u53ca\u8ba1\u7b97\u4e86\u5e73\u5747\u503c<\/li>\n<\/ol>\n

        \u8fd9\u4e2a\u7b80\u5355\u793a\u4f8b\uff0c\u5b8c\u6574\u7684\u4f53\u73b0\u4e86 awk \u7684\u5de5\u4f5c\u673a\u5236\u548c\u539f\u7406\uff0c\u5e0c\u671b\u901a\u8fc7\u6b64\u793a\u4f8b\u80fd\u591f\u5e2e\u4f60\u771f\u6b63\u7406\u89e3 awk \u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n

        \u603b\u7ed3\u5f52\u7eb3<\/strong><\/div>\n

        \u901a\u8fc7\u4e0a\u8ff0\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u5b66\u4e60\u5230\u4e86 awk \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4e0b\u8fb9\u6211\u4eec\u6765\u603b\u7ed3\u4e0b\u51e0\u4e2a\u6982\u5ff5\u548c\u5e38\u7528\u7684\u77e5\u8bc6\u70b9\u3002<\/p>\n

        (\u4e00)\u5185\u7f6e\u53d8\u91cf<\/strong><\/p>\n

        1. \u6bcf\u4e00\u884c\u5185\u5bb9\u8bb0\u5f55\uff0c\u53eb\u505a\u8bb0\u5f55\uff0c\u82f1\u6587\u540d\u79f0 Record<\/p>\n

        2. \u6bcf\u884c\u4e2d\u901a\u8fc7\u5206\u9694\u7b26\u9694\u5f00\u7684\u6bcf\u4e00\u5217\uff0c\u53eb\u505a\u5b57\u6bb5\uff0c\u82f1\u6587\u540d\u79f0 Field<\/p>\n

        \u660e\u786e\u8fd9\u51e0\u4e2a\u6982\u5ff5\u540e\uff0c\u6211\u4eec\u6765\u603b\u7ed3\u51e0\u4e2a\u91cd\u8981\u7684\u5185\u7f6e\u53d8\u91cf\uff1a<\/p>\n

      4. NR\uff1a\u8868\u793a\u5f53\u524d\u7684\u884c\u6570;<\/li>\n
      5. NF\uff1a\u8868\u793a\u5f53\u524d\u7684\u5217\u6570;<\/li>\n
      6. RS\uff1a\u884c\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u662f\u6362\u884c;<\/li>\n
      7. FS\uff1a\u5217\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u662f\u7a7a\u683c\u548c\u5236\u8868\u7b26;<\/li>\n
      8. OFS\uff1a\u8f93\u51fa\u5217\u5206\u9694\u7b26\uff0c\u7528\u4e8e\u6253\u5370\u65f6\u5206\u5272\u5b57\u6bb5\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c<\/li>\n
      9. ORS\uff1a\u8f93\u51fa\u884c\u5206\u9694\u7b26\uff0c\u7528\u4e8e\u6253\u5370\u65f6\u5206\u5272\u8bb0\u5f55\uff0c\u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26<\/li>\n

        (\u4e8c)\u8f93\u51fa\u683c\u5f0f<\/strong><\/p>\n

        awk \u63d0\u4f9b printf \u51fd\u6570\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u529f\u80fd\uff0c\u5177\u4f53\u7684\u4f7f\u7528\u65b9\u5f0f\u548c C \u8bed\u6cd5\u57fa\u672c\u4e00\u81f4\u3002<\/p>\n

        \u57fa\u672c\u7528\u6cd5<\/p>\n

        \"\"<\/p>\n

        \u5e38\u7528\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff1a<\/p>\n

      10. %d \u5341\u8fdb\u5236\u6709\u7b26\u53f7\u6574\u6570<\/li>\n
      11. %u \u5341\u8fdb\u5236\u65e0\u7b26\u53f7\u6574\u6570<\/li>\n
      12. %f \u6d6e\u70b9\u6570<\/li>\n
      13. %s \u5b57\u7b26\u4e32<\/li>\n
      14. %c \u5355\u4e2a\u5b57\u7b26<\/li>\n
      15. %e \u6307\u6570\u5f62\u5f0f\u7684\u6d6e\u70b9\u6570<\/li>\n
      16. %x %X \u65e0\u7b26\u53f7\u4ee5\u5341\u516d\u8fdb\u5236\u8868\u793a\u7684\u6574\u6570<\/li>\n
      17. %0 \u65e0\u7b26\u53f7\u4ee5\u516b\u8fdb\u5236\u8868\u793a\u7684\u6574\u6570<\/li>\n
      18. %g \u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8868\u793a\u6cd5<\/li>\n
      19. \\n \u6362\u884c\u7b26<\/li>\n
      20. \\t Tab\u7b26<\/li>\n

        (\u4e09)\u7f16\u7a0b\u8bed\u53e5<\/strong><\/p>\n

        awk \u4e0d\u4ec5\u662f\u4e00\u4e2a Linux \u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5b83\u5176\u5b9e\u662f\u4e00\u95e8\u811a\u672c\u8bed\u8a00\uff0c\u652f\u6301\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u6240\u6709\u7684\u63a7\u5236\u7ed3\u6784\uff0c\u5b83\u652f\u6301\uff1a<\/p>\n

      21. \u6761\u4ef6\u8bed\u53e5<\/li>\n
      22. \u5faa\u73af\u8bed\u53e5<\/li>\n
      23. \u6570\u7ec4<\/li>\n
      24. \u51fd\u6570<\/li>\n

        (\u56db)\u5e38\u7528\u51fd\u6570<\/strong><\/p>\n

        awk \u5185\u7f6e\u4e86\u5927\u91cf\u7684\u6709\u7528\u51fd\u6570\u529f\u80fd\uff0c\u4e5f\u652f\u6301\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5141\u8bb8\u4f60\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u6269\u5c55\u5185\u7f6e\u51fd\u6570\u3002<\/p>\n

        \u8fd9\u91cc\u53ea\u7b80\u5355\u7f57\u5217\u4e00\u4e9b\u6bd4\u8f83\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u51fd\u6570\uff1a<\/p>\n

      25. index(s, t) \u8fd4\u56de\u5b50\u4e32 t \u5728 s \u4e2d\u7684\u4f4d\u7f6e<\/li>\n
      26. length(s) \u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u957f\u5ea6<\/li>\n
      27. split(s, a, sep) \u5206\u5272\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5206\u5272\u540e\u7684\u5404\u5b57\u6bb5\u5b58\u653e\u5728\u6570\u7ec4 a \u4e2d<\/li>\n
      28. substr(s, p, n) \u6839\u636e\u53c2\u6570\uff0c\u8fd4\u56de\u5b50\u4e32<\/li>\n
      29. tolower(s) \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199<\/li>\n
      30. toupper(s) \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/li>\n

        \u8fd9\u91cc\u53ea\u7b80\u5355\u603b\u7ed3\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u51fd\u6570\uff0c\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\uff0c\u8fd8\u9700\u8981\u4f60\u53c2\u7167\u524d\u8fb9\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u4e3e\u4e00\u53cd\u4e09\uff0c\u8fd0\u7528\u5230\u5b9e\u9645\u95ee\u9898\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u6211\u4eec\u77e5\u9053 Linux \u4e09\u5251\u5ba2\uff0c\u5b83\u4eec\u662f grep\u3001sed\u3001awk\u3002\u5728\u524d\u8fb9\u5df2\u7ecf\u8bb2\u8fc7 grep \u548c sed\uff0c\u6ca1\u770b\u8fc7\u7684 […]<\/p>\n","protected":false},"author":317,"featured_media":203111,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-213674","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\/213674","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\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=213674"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213674\/revisions"}],"predecessor-version":[{"id":213699,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213674\/revisions\/213699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/203111"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=213674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=213674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=213674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}