{"id":25549,"date":"2023-10-16T10:18:05","date_gmt":"2023-10-16T02:18:05","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=25549"},"modified":"2023-10-16T10:18:05","modified_gmt":"2023-10-16T02:18:05","slug":"awk-begin-end","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-begin-end.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 awk \u7684\u7279\u6b8a\u6a21\u5f0f BEGIN \u4e0e END"},"content":{"rendered":"

\u5728 awk \u7cfb\u5217\u7684\u7b2c\u516b\u8282\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684 awk \u547d\u4ee4\u529f\u80fd\uff0c\u5b83\u4eec\u662f\u53d8\u91cf\u3001\u6570\u5b57\u8868\u8fbe\u5f0f\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002<\/p>\n

\u672c\u8282\u6211\u4eec\u5c06\u5b66\u4e60\u66f4\u591a\u7684 awk \u529f\u80fd\uff0c\u5373 awk \u7684\u7279\u6b8a\u6a21\u5f0f\uff1aBEGIN<\/span> \u548c END<\/span> \u3002<\/p>\n

\u968f\u7740\u6211\u4eec\u9010\u6e10\u5c55\u5f00\uff0c\u5e76\u63a2\u7d22\u51fa\u66f4\u591a\u6784\u5efa\u590d\u6742 awk \u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u5c06\u4f1a\u8bc1\u660e awk \u7684\u8fd9\u4e9b\u7279\u6b8a\u529f\u80fd\u7684\u662f\u591a\u4e48\u5f3a\u5927\u3002<\/p>\n

\u5f00\u59cb\u524d\uff0c\u5148\u8ba9\u6211\u4eec\u56de\u987e\u4e00\u4e0b awk \u7cfb\u5217\u7684\u4ecb\u7ecd\uff0c\u8bb0\u5f97\u5f53\u6211\u4eec\u5f00\u59cb\u8fd9\u4e2a\u7cfb\u5217\u65f6\uff0c\u6211\u5c31\u6307\u51fa awk \u6307\u4ee4\u7684\u901a\u7528\u8bed\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

# awk 'script' filenames  \r\n<\/pre>\n

\u5728\u4e0a\u8ff0\u8bed\u6cd5\u4e2d\uff0cawk \u811a\u672c\u62e5\u6709\u8fd9\u6837\u7684\u5f62\u5f0f\uff1a<\/p>\n

\/pattern\/ { actions } \r\n<\/pre>\n

\u4f60\u901a\u5e38\u4f1a\u53d1\u73b0\u811a\u672c\u4e2d\u7684\u6a21\u5f0f\uff08\/pattern<\/span> \uff09\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6b64\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u8fd9\u91cc\u7528\u7279\u6b8a\u6a21\u5f0f\u00a0BENGIN<\/span> \u548c END<\/span> \u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e5f\u80fd\u6309\u7167\u4e0b\u9762\u7684\u5f62\u5f0f\u7f16\u5199\u4e00\u6761 awk \u547d\u4ee4\uff1a<\/p>\n

awk '\r\nBEGIN { actions } \r\n\/pattern\/ { actions }\r\n\/pattern\/ { actions }\r\n\u2026\u2026\u2026.\r\nEND { actions } \r\n' filenames  \r\n<\/pre>\n

\u5047\u5982\u4f60\u5728 awk \u811a\u672c\u4e2d\u4f7f\u7528\u4e86\u7279\u6b8a\u6a21\u5f0f\uff1aBEGIN<\/span> \u548c END<\/span> \uff0c\u4ee5\u4e0b\u5219\u662f\u5b83\u4eec\u5bf9\u5e94\u7684\u542b\u4e49\uff1a<\/p>\n