{"id":22374,"date":"2024-08-21T03:05:10","date_gmt":"2024-08-20T19:05:10","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=22374"},"modified":"2024-08-21T03:05:10","modified_gmt":"2024-08-20T19:05:10","slug":"awk-print-field-cols","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-print-field-cols.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 awk \u8f93\u51fa\u6587\u672c\u4e2d\u7684\u5b57\u6bb5\u548c\u5217"},"content":{"rendered":"

\u9996\u5148\u6211\u4eec\u8981\u77e5\u9053\uff0cawk \u80fd\u591f\u81ea\u52a8\u5c06\u8f93\u5165\u7684\u884c\uff0c\u5206\u9694\u4e3a\u82e5\u5e72\u5b57\u6bb5\u3002\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u5c31\u662f\u4e00\u7ec4\u5b57\u7b26\uff0c\u5b83\u4eec\u548c\u5176\u4ed6\u7684\u5b57\u6bb5\u7531\u4e00\u4e2a\u5185\u90e8\u5b57\u6bb5\u5206\u9694\u7b26\u5206\u9694\u5f00\u6765\u3002<\/p>\n

\u5982\u679c\u4f60\u719f\u6089 Unix\/Linux \u6216\u8005\u61c2\u5f97 bash shell \u7f16\u7a0b\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u77e5\u9053\u4ec0\u4e48\u662f\u5185\u90e8\u5b57\u6bb5\u5206\u9694\u7b26\uff08IFS\uff09\u53d8\u91cf\u3002awk \u4e2d\u9ed8\u8ba4\u7684 IFS \u662f\u5236\u8868\u7b26\u548c\u7a7a\u683c\u3002<\/p>\n

awk \u4e2d\u7684\u5b57\u6bb5\u5206\u9694\u7b26\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a\u5f53\u8bfb\u5230\u4e00\u884c\u8f93\u5165\u65f6\uff0c\u5c06\u5b83\u6309\u7167\u6307\u5b9a\u7684 IFS \u5206\u5272\u4e3a\u4e0d\u540c\u5b57\u6bb5\uff0c\u7b2c\u4e00\u7ec4\u5b57\u7b26\u5c31\u662f\u5b57\u6bb5\u4e00\uff0c\u53ef\u4ee5\u901a\u8fc7 $1 \u6765\u8bbf\u95ee\uff0c\u7b2c\u4e8c\u7ec4\u5b57\u7b26\u5c31\u662f\u5b57\u6bb5\u4e8c\uff0c\u53ef\u4ee5\u901a\u8fc7 $2 \u6765\u8bbf\u95ee\uff0c\u7b2c\u4e09\u7ec4\u5b57\u7b26\u5c31\u662f\u5b57\u6bb5\u4e09\uff0c\u53ef\u4ee5\u901a\u8fc7 $3 \u6765\u8bbf\u95ee\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u6700\u540e\u4e00\u7ec4\u5b57\u7b26\u3002<\/p>\n

\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3 awk \u7684\u5b57\u6bb5\u7f16\u8f91\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n

\u4f8b 1<\/strong><\/span>\uff1a\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a tecmintinfo.txt \u7684\u6587\u672c\u6587\u4ef6\u3002<\/p>\n

# vi tecmintinfo.txt\r\n# cat tecmintinfo.txt\r\n<\/pre>\n

\"awk<\/p>\n

\u5728 Linux \u4e0a\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6<\/em><\/p>\n

\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u6211\u8bd5\u7740\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u4ece\u6587\u672c tecmintinfo.txt \u4e2d\u8f93\u51fa\u7b2c\u4e00\u4e2a\uff0c\u7b2c\u4e8c\u4e2a\uff0c\u4ee5\u53ca\u7b2c\u4e09\u4e2a\u5b57\u6bb5\u3002<\/p>\n

$ awk '\/\/{print $1 $2 $3 }' tecmintinfo.txt\r\nTecMint.comisthe\r\n<\/pre>\n

\u4ece\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\uff0c\u524d\u4e09\u4e2a\u5b57\u6bb5\u7684\u5b57\u7b26\u662f\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\u8f93\u51fa\u7684\uff1a<\/p>\n