{"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 \u5728\u4e0a\u8ff0\u8bed\u6cd5\u4e2d\uff0cawk \u811a\u672c\u62e5\u6709\u8fd9\u6837\u7684\u5f62\u5f0f\uff1a<\/p>\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 \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 \u542b\u6709\u8fd9\u4e9b\u7279\u6b8a\u6a21\u5f0f\u7684 awk \u547d\u4ee4\u811a\u672c\u7684\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n \u5f53\u4f60\u4f7f\u7528\u7279\u6b8a\u6a21\u5f0f\u65f6\uff0c\u60f3\u8981\u5728 awk \u64cd\u4f5c\u4e2d\u83b7\u5f97\u6700\u597d\u7684\u7ed3\u679c\uff0c\u4f60\u5e94\u5f53\u8bb0\u4f4f\u4e0a\u9762\u7684\u6267\u884c\u987a\u5e8f\u3002<\/p>\n \u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528\u7b2c\u516b\u8282\u7684\u4f8b\u5b50\u8fdb\u884c\u6f14\u793a\uff0c\u90a3\u4e2a\u4f8b\u5b50\u662f\u5173\u4e8e Tecmint \u62e5\u6709\u7684\u57df\u540d\u5217\u8868\uff0c\u5e76\u4fdd\u5b58\u5728\u4e00\u4e2a\u53eb\u505a domains.txt \u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n <\/p>\n \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u7edf\u8ba1\u51fa domains.txt \u6587\u4ef6\u4e2d\u57df\u540dtecmint.com<\/span> \u51fa\u73b0\u7684\u6b21\u6570\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 shell \u811a\u672c\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u4efb\u52a1\uff0c\u5b83\u4f7f\u7528\u4e86\u53d8\u91cf\u3001\u6570\u5b66\u8868\u8fbe\u5f0f\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u601d\u60f3\uff0c\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n \u73b0\u5728\u8ba9\u6211\u4eec\u50cf\u4e0b\u9762\u8fd9\u6837\u5728\u4e0a\u8ff0\u811a\u672c\u7684 awk \u547d\u4ee4\u4e2d\u5e94\u7528\u8fd9\u4e24\u4e2a\u7279\u6b8a\u6a21\u5f0f\uff1aBEGIN<\/span> \u548c END<\/span> \uff1a<\/p>\n \u6211\u4eec\u5e94\u5f53\u628a\u811a\u672c\uff1a<\/p>\n \u6539\u6210\uff1a<\/p>\n \u5728\u4fee\u6539\u4e86 awk \u547d\u4ee4\u4e4b\u540e\uff0c\u73b0\u5728\u5b8c\u6574\u7684 shell \u811a\u672c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n <\/p>\n \u5f53\u6211\u4eec\u8fd0\u884c\u4e0a\u9762\u7684\u811a\u672c\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u8f93\u51fa domains.txt \u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u6267\u884c awk \u547d\u4ee4\u811a\u672c\uff0c\u8be5\u547d\u4ee4\u811a\u672c\u4e2d\u7684\u7279\u6b8a\u6a21\u5f0f BEGIN<\/span> \u5c06\u4f1a\u5728\u4ece\u6587\u4ef6\u8bfb\u53d6\u4efb\u4f55\u884c\u4e4b\u524d\u5e2e\u52a9\u6211\u4eec\u8f93\u51fa\u8fd9\u6837\u7684\u6d88\u606f\u201c\u6587\u4ef6\u4e2d\u51fa\u73b0 tecmint.com \u7684\u6b21\u6570\u662f\uff1a<\/span> \u201d\u3002<\/p>\n \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7684\u6a21\u5f0f\/^tecmint.com\/<\/span> \u4f1a\u5728\u6bcf\u4e2a\u8f93\u5165\u884c\u4e2d\u8fdb\u884c\u6bd4\u8f83\uff0c\u5bf9\u5e94\u7684\u52a8\u4f5c{ counter+=1 ; }<\/span> \u4f1a\u5728\u6bcf\u4e2a\u5339\u914d\u6210\u529f\u7684\u884c\u4e0a\u6267\u884c\uff0c\u5b83\u4f1a\u7edf\u8ba1\u51fa tecmint.com<\/span> \u5728\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n \u6700\u7ec8\uff0cEND<\/span> \u6a21\u5f0f\u5c06\u4f1a\u8f93\u51fa\u57df\u540d tecmint.com<\/span> \u5728\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u603b\u6b21\u6570\u3002<\/p>\n <\/p>\n \u6700\u540e\u603b\u7ed3\u4e00\u4e0b\uff0c\u6211\u4eec\u5728\u672c\u8282\u4e2d\u6f14\u793a\u4e86\u66f4\u591a\u7684 awk \u529f\u80fd\uff0c\u5e76\u5b66\u4e60\u4e86\u7279\u6b8a\u6a21\u5f0f BEGIN<\/span> \u548c END<\/span> \u7684\u6982\u5ff5\u3002<\/p>\n \u6b63\u5982\u6211\u4e4b\u524d\u6240\u8a00\uff0c\u8fd9\u4e9b awk \u529f\u80fd\u5c06\u4f1a\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u51fa\u66f4\u590d\u6742\u7684\u6587\u672c\u8fc7\u6ee4\u64cd\u4f5c\u3002\u7b2c\u5341\u8282\u5c06\u4f1a\u7ed9\u51fa\u66f4\u591a\u7684 awk \u529f\u80fd\uff0c\u6211\u4eec\u5c06\u4f1a\u5b66\u4e60 awk \u5185\u7f6e\u53d8\u91cf\u7684\u601d\u60f3\uff0c\u6240\u4ee5\uff0c\u8bf7\u7ee7\u7eed\u4fdd\u6301\u5173\u6ce8\u3002<\/p>\n via: http:\/\/www.tecmint.com\/learn-use-awk-special-patterns-begin-and-end\/<\/p>\n \u4f5c\u8005\uff1aAaron Kili \u8bd1\u8005\uff1aChrisLeeGit \u6821\u5bf9\uff1awxy<\/p>\n \u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-7654-1.html<\/a><\/p>\n# awk 'script' filenames \r\n<\/pre>\n
\/pattern\/ { actions } \r\n<\/pre>\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
\n
\n
news.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\n<\/pre>\n
$ cat ~\/domains.txt\r\n<\/pre>\n
#!\/bin\/bash\r\nfor file in $@; do\r\nif [ -f $file ] ; then\r\n### \u8f93\u51fa\u6587\u4ef6\u540d\r\necho \"File is: $file\"\r\n### \u8f93\u51fa\u4e00\u4e2a\u9012\u589e\u7684\u6570\u5b57\u8bb0\u5f55\u5305\u542b tecmint.com \u7684\u884c\u6570\r\nawk '\/^tecmint.com\/ { counter+=1 ; printf \"%s\/n\", counter ; }' $file\r\nelse\r\n### \u82e5\u8f93\u5165\u4e0d\u662f\u6587\u4ef6\uff0c\u5219\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\r\necho \"$file \u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bf7\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u3002\" >&2 && exit 1\r\nfi\r\ndone\r\n### \u6210\u529f\u6267\u884c\u540e\u4f7f\u7528\u9000\u51fa\u4ee3\u7801 0 \u7ec8\u6b62\u811a\u672c\r\nexit 0\r\n<\/pre>\n
awk '\/^tecmint.com\/ { counter+=1 ; printf \"%s\/n\", counter ; }' $file\r\n<\/pre>\n
awk ' BEGIN { print \"\u6587\u4ef6\u4e2d\u51fa\u73b0 tecmint.com \u7684\u6b21\u6570\u662f\uff1a\" ; }\r\n\/^tecmint.com\/ { counter+=1 ; }\r\nEND { printf \"%s\/n\", counter ; } \r\n' $file\r\n<\/pre>\n
#!\/bin\/bash\r\nfor file in $@; do\r\nif [ -f $file ] ; then\r\n### \u8f93\u51fa\u6587\u4ef6\u540d\r\necho \"File is: $file\"\r\n### \u8f93\u51fa\u6587\u4ef6\u4e2d tecmint.com \u51fa\u73b0\u7684\u603b\u6b21\u6570\r\nawk ' BEGIN { print \"\u6587\u4ef6\u4e2d\u51fa\u73b0 tecmint.com \u7684\u6b21\u6570\u662f\uff1a\" ; }\r\n\/^tecmint.com\/ { counter+=1 ; }\r\nEND { printf \"%s\/n\", counter ; } \r\n' $file\r\nelse\r\n### \u82e5\u8f93\u5165\u4e0d\u662f\u6587\u4ef6\uff0c\u5219\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\r\necho \"$file \u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bf7\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u3002\" >&2 && exit 1\r\nfi\r\ndone\r\n### \u6210\u529f\u6267\u884c\u540e\u4f7f\u7528\u9000\u51fa\u4ee3\u7801 0 \u7ec8\u6b62\u811a\u672c\r\nexit 0\r\n<\/pre>\n
$ .\/script.sh ~\/domains.txt \r\n<\/pre>\n
\n\n