{"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 <\/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 \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 \u5982\u679c\u4f60\u6ce8\u610f\u89c2\u5bdf\u8f93\u51fa\u7684\u8bdd\u53ef\u4ee5\u53d1\u73b0\uff0c\u8f93\u51fa\u7684\u5b57\u6bb5\u503c\u5e76\u6ca1\u6709\u88ab\u5206\u9694\u5f00\uff0c\u8fd9\u662f print \u51fd\u6570\u9ed8\u8ba4\u7684\u884c\u4e3a\u3002<\/p>\n \u4e3a\u4e86\u4f7f\u8f93\u51fa\u770b\u5f97\u66f4\u6e05\u695a\uff0c\u8f93\u51fa\u7684\u5b57\u6bb5\u503c\u4e4b\u95f4\u4f7f\u7528\u7a7a\u683c\u5206\u5f00\uff0c\u4f60\u9700\u8981\u6dfb\u52a0 (,) \u64cd\u4f5c\u7b26\u3002<\/p>\n \u9700\u8981\u8bb0\u4f4f\u800c\u4e14\u975e\u5e38\u91cd\u8981\u7684\u662f\uff0c($) \u5728 awk \u548c\u5728 shell \u811a\u672c\u4e2d\u7684\u4f7f\u7528\u662f\u622a\u7136\u4e0d\u540c\u7684\uff01<\/p>\n \u5728 shell \u811a\u672c\u4e2d\uff0c($) \u88ab\u7528\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u503c\u3002\u800c\u5728 awk \u4e2d\uff0c($) \u53ea\u6709\u5728\u83b7\u53d6\u5b57\u6bb5\u7684\u503c\u65f6\u624d\u4f1a\u7528\u5230\uff0c\u4e0d\u80fd\u7528\u4e8e\u83b7\u53d6\u53d8\u91cf\u7684\u503c\u3002<\/p>\n \u4f8b 2<\/strong><\/span>\uff1a\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7528\u5230\u4e86\u4e00\u4e2a\u540d\u4e3a my_shoping.list \u7684\u5305\u542b\u591a\u884c\u7684\u6587\u4ef6\u3002<\/p>\n \u5982\u679c\u4f60\u53ea\u60f3\u8f93\u51fa\u8d2d\u7269\u6e05\u5355\u4e0a\u6bcf\u4e00\u4e2a\u7269\u54c1\u7684\u5355\u4ef7\uff0c\u4f60\u53ea\u9700\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n \u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u7684\u8f93\u51fa\u4e0d\u591f\u6e05\u6670\uff0cawk \u8fd8\u6709\u4e00\u4e2a printf \u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u8f93\u51fa\u683c\u5f0f\u5316\u3002<\/p>\n \u4f7f\u7528 printf \u6765\u683c\u5f0f\u5316 Item_Name \u548c Unit_Price \u7684\u8f93\u51fa\uff1a<\/p>\n \u4f7f\u7528 awk \u8fc7\u6ee4\u6587\u672c\u6216\u5b57\u7b26\u4e32\u65f6\uff0c\u5b57\u6bb5\u7f16\u8f91\u7684\u529f\u80fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5b83\u80fd\u591f\u5e2e\u52a9\u4f60\u4ece\u4e00\u4e2a\u8868\u7684\u6570\u636e\u4e2d\u5f97\u5230\u7279\u5b9a\u7684\u5217\u3002\u4e00\u5b9a\u8981\u8bb0\u4f4f\u7684\u662f\uff0cawk \u4e2d ($) \u64cd\u4f5c\u7b26\u7684\u7528\u6cd5\u4e0e\u5176\u5728 shell \u811a\u672c\u4e2d\u7684\u7528\u6cd5\u662f\u4e0d\u540c\u7684\uff01<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-7587-1.html<\/a><\/p>\n# vi tecmintinfo.txt\r\n# cat tecmintinfo.txt\r\n<\/pre>\n
$ awk '\/\/{print $1 $2 $3 }' tecmintinfo.txt\r\nTecMint.comisthe\r\n<\/pre>\n
\n
$ awk '\/\/{print $1, $2, $3; }' tecmintinfo.txt\r\n\r\nTecMint.com is the\r\n<\/pre>\n
No Item_Name Unit_Price Quantity Price\r\n1 Mouse #20,000 1 #20,000\r\n2 Monitor #500,000 1 #500,000\r\n3 RAM_Chips #150,000 2 #300,000\r\n4 Ethernet_Cables #30,000 4 #120,000 \r\n<\/pre>\n
$ awk '\/\/{print $2, $3 }' my_shopping.txt \r\n\r\nItem_Name Unit_Price\r\nMouse #20,000\r\nMonitor #500,000\r\nRAM_Chips #150,000\r\nEthernet_Cables #30,000\r\n<\/pre>\n
$ awk '\/\/{printf \"%-10s %s\/n\",$2, $3 }' my_shopping.txt \r\n\r\nItem_Name Unit_Price\r\nMouse #20,000\r\nMonitor #500,000\r\nRAM_Chips #150,000\r\nEthernet_Cables #30,000\r\n<\/pre>\n
\n