{"id":15939,"date":"2023-09-18T04:10:08","date_gmt":"2023-09-17T20:10:08","guid":{"rendered":"https:\/\/www.linuxcool.com\/lmlqsdywjml"},"modified":"2023-09-18T04:10:08","modified_gmt":"2023-09-17T20:10:08","slug":"lmlqsdywjml","status":"publish","type":"post","link":"https:\/\/www.linuxcool.com\/lmlqsdywjml","title":{"rendered":"Linux Awk\u547d\u4ee4\u8f7b\u677e\u6253\u5370\u6587\u4ef6\u67d0\u5217"},"content":{"rendered":"<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u5728<a style='color:#0000CC' href='https:\/\/www.linuxprobe.com\/' target='_blank' rel=\"noopener\">Linux<\/a><a style='color:#0000CC' href='https:\/\/www.linuxdown.com\/' target='_blank' rel=\"noopener\">\u7cfb\u7edf<\/a>\u4e2d\uff0cAwk\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u6790\u548c\u5904\u7406\u3002\u5176\u4e2dlinux\u7cfb\u7edf\u547d\u4ee4\uff0c\u6253\u5370\u67d0\u5217\u662fAwk<a style='color:#0000CC' href='https:\/\/www.linuxcool.com\/' target='_blank' rel=\"noopener\">\u547d\u4ee4<\/a>\u4e2d\u5e38\u7528\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Linux Awk\u547d\u4ee4\u6765\u6253\u5370\u6307\u5b9a\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u3002<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>1.\u5b89\u88c5\u548c\u542f\u52a8Awk\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Linux\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86Awk\u5de5\u5177\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nsudo apt-get install gawk\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u8f93\u5165`awk`\u547d\u4ee4\u6765\u542f\u52a8Awk\u3002<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>2.\u4f7f\u7528Awk\u6253\u5370\u67d0\u5217\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u8981\u4f7f\u7528Awk\u6253\u5370\u67d0\u5217\uff0c\u9700\u8981\u4f7f\u7528`-F`\u53c2\u6570\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u5e76\u7528`{print}`\u8bed\u53e5\u6307\u5b9a\u8981\u6253\u5370\u7684\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3adata.txt\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\n\u59d3\u540d\u5e74\u9f84\u6027\u522b\n\u5f20\u4e09 20 \u7537\n\u674e\u56db 25 \u5973\n\u738b\u4e94 30 \u7537\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u5982\u679c\u6211\u4eec\u60f3\u8981\u6253\u5370\u7b2c\u4e8c\u5217\uff08\u5e74\u9f84\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F'''{print $2}' data.txt\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u6267\u884c\u4ee5\u4e0a\u547d\u4ee4\u540e\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\n\u5e74\u9f84\n20\n25\n30\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>3.\u6307\u5b9a\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u4f46\u5b9e\u9645\u4e0a\uff0cAwk\u652f\u6301\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\u6765\u89e3\u6790\u6587\u672c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u6570\u636e\u6587\u4ef6\u4ee5\u9017\u53f7\u5206\u9694\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5370\u7b2c\u4e8c\u5217\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F',''{print $2}' data.txt\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>4.\u6253\u5370\u591a\u5217\u6570\u636e\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u9664\u4e86\u6253\u5370\u5355\u72ec\u7684\u4e00\u5217\uff0cAwk\u8fd8\u53ef\u4ee5\u540c\u65f6\u6253\u5370\u591a\u5217\u6570\u636e\u3002\u53ea\u9700\u8981\u5728`{print}`\u8bed\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5217\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u540c\u65f6\u6253\u5370\u59d3\u540d\u548c\u6027\u522b\u8fd9\u4e24\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F'''{print $1,$3}' data.txt\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><img style='max-height:350px;max-width:80%;margin: 10px auto' src='https:\/\/www.linuxcool.com\/wp-content\/uploads\/2023\/09\/1694981407399_0.jpg' alt='linux awk\u6253\u5370\u67d0\u5217\u547d\u4ee4_awk\u547d\u4ee4\u8be6\u89e3' \/><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>5.\u6253\u5370\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u67d0\u5217\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>Awk\u8fd8\u652f\u6301\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5e76\u6253\u5370\u7b26\u5408\u6761\u4ef6\u7684\u67d0\u4e00\u5217\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u6253\u5370\u5e74\u9f84\u5927\u4e8e25\u5c81\u7684\u4eba\u7684\u59d3\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F'''$2&gt; 25 {print $1}' data.txt\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u6267\u884c\u4ee5\u4e0a\u547d\u4ee4\u540e\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\n\u738b\u4e94\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>6.\u6253\u5370\u8ba1\u7b97\u7ed3\u679c\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><img style='max-height:350px;max-width:80%;margin: 10px auto' src='https:\/\/www.linuxcool.com\/wp-content\/uploads\/2023\/09\/1694981407399_1.png' alt='linux awk\u6253\u5370\u67d0\u5217\u547d\u4ee4_awk\u547d\u4ee4\u8be6\u89e3' \/><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u9664\u4e86\u76f4\u63a5\u6253\u5370\u6587\u672c\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\uff0cAwk\u8fd8\u652f\u6301\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u5e76\u6253\u5370\u8ba1\u7b97\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u8ba1\u7b97\u5e74\u9f84\u7684\u5e73\u5747\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765<strong>linux awk\u6253\u5370\u67d0\u5217\u547d\u4ee4<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F'''{sum +=$2} END {print sum\/NR}' data.txt\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u6267\u884c\u4ee5\u4e0a\u547d\u4ee4\u540e<strong>linux awk\u6253\u5370\u67d0\u5217\u547d\u4ee4<\/strong>\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\n25\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>7.\u5904\u7406\u5176\u4ed6\u683c\u5f0f\u7684\u6587\u672c\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u9664\u4e86\u5904\u7406\u7a7a\u683c\u6216\u9017\u53f7\u5206\u9694\u7684\u6587\u672c\u6587\u4ef6\u5916\uff0cAwk\u8fd8\u53ef\u4ee5\u5904\u7406\u5176\u4ed6\u683c\u5f0f\u7684\u6587\u672c\u6587\u4ef6\u3002\u53ea\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u6307\u5b9a\u6b63\u786e\u7684\u5206\u9694\u7b26\u5373\u53ef\u3002\u4f8b\u5982linux\u67e5\u770b\u786c\u4ef6\u4fe1\u606f\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u4ee5\u5236\u8868\u7b26\u5206\u9694\u7684\u6570\u636e\u6587\u4ef6data.tsv\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5370\u7b2c\u4e09\u5217\uff1a<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><img style='max-height:350px;max-width:80%;margin: 10px auto' src='https:\/\/www.linuxcool.com\/wp-content\/uploads\/2023\/09\/1694981407399_2.jpg' alt='linux awk\u6253\u5370\u67d0\u5217\u547d\u4ee4_awk\u547d\u4ee4\u8be6\u89e3' \/><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\n<pre>\nawk -F't''{print $3}' data.tsv\n<\/pre>\n<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>8.\u5e94\u7528\u4e3e\u4f8b\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u4ee5\u4e0a\u662f\u5173\u4e8e\u4f7f\u7528Awk\u6253\u5370\u67d0\u5217\u7684\u57fa\u672c\u4ecb\u7ecd\u548c\u793a\u4f8b\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cAwk\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6Linux\u547d\u4ee4\u7ed3\u5408\u4f7f\u7528\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Awk\u5c06\u67d0\u4e2a\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\u63d0\u53d6\u51fa\u6765\u5e76\u6253\u5370\u51fa\u6765\uff0c\u65b9\u4fbf\u8fdb\u884c\u6545\u969c\u6392\u67e5\u3002<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'><b>9.\u603b\u7ed3\uff1a<\/b><\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Linux Awk\u547d\u4ee4\u6765\u6253\u5370\u6307\u5b9a\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u3002\u65e0\u8bba\u662f\u7b80\u5355\u5730\u6253\u5370\u4e00\u5217\u8fd8\u662f\u8fdb\u884c\u590d\u6742\u7684\u6761\u4ef6\u8fc7\u6ee4\u548c\u8ba1\u7b97\uff0cAwk\u90fd\u80fd\u591f\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u9700\u6c42\u3002\u638c\u63e1\u4e86Awk\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6587\u672c\u6570\u636e\u3002<\/p>\n<p style='margin-bottom:16px;color:#555555;font-size:16px;line-height:200%;text-indent:2em'>\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u5728Linux\u7cfb\u7edf\u4e2d\u4f7f\u7528Awk\u547d\u4ee4\u6253\u5370\u67d0\u5217\u6570\u636e\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5176\u4e2d\uff0c\u6253\u5370\u67d0\u5217\u662fAwk\u547d\u4ee4\u4e2d\u5e38\u7528\u7684\u529f\u80fd\u4e4b\u4e00\u3002Awk\u547d\u4ee4\u6765\u6253\u5370\u6307\u5b9a\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u3002\u8981\u4f7f\u7528Awk\u6253\u5370\u67d0\u5217\uff0c\u9700\u8981\u4f7f\u7528`-F`\u53c2\u6570\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u5e76\u7528`{print}`\u8bed\u53e5\u6307\u5b9a\u8981\u6253\u5370\u7684\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u6570\u636e\u6587\u4ef6\u4ee5\u9017\u53f7\u5206\u9694\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5370\u7b2c\u4e8c\u5217\uff1atsv\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5370\u7b2c\u4e09\u5217\uff1aAwk\u547d\u4ee4\u6765\u6253\u5370\u6307\u5b9a\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u5728Linux\u7cfb\u7edf\u4e2d\u4f7f\u7528Awk\u547d\u4ee4\u6253\u5370\u67d0\u5217\u6570\u636e\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":15940,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[666,37,364,116,662],"class_list":["post-15939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-fg","tag-ml","tag-dy","tag-zd-2","tag-wb-2"],"_links":{"self":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/15939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/comments?post=15939"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/15939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/media\/15940"}],"wp:attachment":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/media?parent=15939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/categories?post=15939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/tags?post=15939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}