{"id":9653,"date":"2025-04-18T17:29:55","date_gmt":"2025-04-18T09:29:55","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/9653.html"},"modified":"2025-04-18T17:29:55","modified_gmt":"2025-04-18T09:29:55","slug":"awk%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/9653.html","title":{"rendered":"awk\u8be6\u89e3"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e00\u3001\u6982\u8ff0<\/li>\n<li>\u4e8c\u3001\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u4e09\u3001\u5de5\u4f5c\u6d41\u7a0b<\/li>\n<li>\n<ul>\n<li>3.1 \u8fd0\u884c\u6a21\u5f0f<\/li>\n<li>3.2 \u6267\u884c\u6d41\u7a0b<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u57fa\u672c\u8bed\u6cd5<\/li>\n<li>\n<ul>\n<li>4.1 \u547d\u4ee4\u683c\u5f0f<\/li>\n<\/ul>\n<\/li>\n<li>\u4e94\u3001\u5b9e\u6218\u6848\u4f8b<\/li>\n<li>\n<ul>\n<li>5.1 \u5f00\u59cb\u5757&#xff08;BEGIN block&#xff09;<\/li>\n<li>\n<ul>\n<li>5.1.1print<\/li>\n<\/ul>\n<\/li>\n<li>5.2 \u5185\u7f6e\u53d8\u91cf<\/li>\n<li>5.3 BEGIN END \u8fd0\u7b97<\/li>\n<li>\n<ul>\n<li>5.3.1 \u6269\u5c55\u751f\u4ea7&#xff1a;\u7f51\u5361\u7684ip\u3001\u6d41\u91cf<\/li>\n<li>5.3.2 \u6839\u5206\u533a\u7684\u53ef\u7528\u91cf<\/li>\n<li>5.3.3 awk\u7684\u8fd0\u7b97&#xff1a;<\/li>\n<li>5.3.4\u6848\u4f8b<\/li>\n<li>5.3.5 \u6848\u4f8b<\/li>\n<li>5.3.6 FS<\/li>\n<li>5.3.7 OFS<\/li>\n<li>5.3.8 FNR<\/li>\n<li>5.3.9 NR<\/li>\n<li>5.3.10 RS<\/li>\n<li>5.3.11 ORS<\/li>\n<\/ul>\n<\/li>\n<li>5.4 \u5f15\u7528\u53d8\u91cf<\/li>\n<li>\n<ul>\n<li>5.4.1\u6848\u4f8b<\/li>\n<li>5.4.2\u6ce8&#xff1a;\u5148\u505a\u5982\u4e0b\u5b9e\u9a8c<\/li>\n<\/ul>\n<\/li>\n<li>5.5CPU\u4f7f\u7528\u7387&#xff08;\u7b97\u4e0d&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\u516d\u3001\u6700\u540e\u603b\u7ed3<\/li>\n<li>\n<ul>\n<li>5.5CPU\u4f7f\u7528\u7387&#xff08;\u7b97\u4e0d&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\u516d\u3001\u6700\u540e\u603b\u7ed3<\/li>\n<\/ul>\n<h2>\u4e00\u3001\u6982\u8ff0<\/h2>\n<p>\u200b AWK \u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00&#xff0c;\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002\u5b83\u662f\u4e13\u95e8\u4e3a\u6587\u672c\u5904\u7406\u8bbe\u8ba1\u7684\u7f16\u7a0b\u8bed\u8a00&#xff0c;\u4e5f\u662f\u884c\u5904\u7406\u8f6f\u4ef6&#xff0c;\u901a\u5e38\u7528\u4e8e\u626b\u63cf\u3001\u8fc7\u6ee4\u3001\u7edf\u8ba1\u6c47\u603b\u5de5\u4f5c\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6807\u51c6\u8f93\u5165\u4e5f\u53ef\u4ee5\u662f\u7ba1\u9053\u6216\u6587\u4ef6\u3002<\/p>\n<p>\u200b 20\u4e16\u7eaa70\u5e74\u4ee3\u8bde\u751f\u4e8e\u8d1d\u5c14\u5b9e\u9a8c\u5ba4&#xff0c;\u73b0\u5728centos7\u7528\u7684\u662fgawk\u4e4b\u6240\u4ee5\u53eb AWK \u662f\u56e0\u4e3a\u5176\u53d6\u4e86\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho&#xff0c;Peter Weinberger, \u548c Brian Kernighan \u7684 Family Name \u7684\u9996\u5b57\u7b26\u3002AWK&#xff1a;Aho, Weinberger, Kernighan&#xff0c;\u62a5\u544a\u751f\u6210\u5668&#xff0c;\u683c\u5f0f\u5316\u6587\u672c\u8f93\u51fa&#xff0c;GNU\/Linux\u53d1\u5e03\u7684AWK\u76ee\u524d\u7531\u81ea\u7531\u8f6f\u4ef6\u57fa\u91d1\u4f1a&#xff08;FSF&#xff09;\u8fdb\u884c\u5f00\u53d1\u548c\u7ef4\u62a4&#xff0c;\u901a\u5e38\u4e5f\u79f0\u5b83\u4e3a GNU AWK\u3002<\/p>\n<p>\u6709\u591a\u79cd\u7248\u672c&#xff1a;<\/p>\n<ul>\n<li>AWK&#xff1a;\u539f\u5148\u6765\u6e90\u4e8e AT &amp; T \u5b9e\u9a8c\u5ba4\u7684\u7684AWK<\/li>\n<li>NAWK&#xff1a;New awk&#xff0c;AT &amp; T \u5b9e\u9a8c\u5ba4\u7684AWK\u7684\u5347\u7ea7\u7248<\/li>\n<li>GAWK&#xff1a;\u5373GNU AWK\u3002\u6240\u6709\u7684GNU\/Linux\u53d1\u5e03\u7248\u90fd\u81ea\u5e26GAWK&#xff0c;\u5b83\u4e0eAWK\u548cNAWK\u5b8c\u5168\u517c\u5bb9<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u200b \u5f53\u8bfb\u5230\u7b2c\u4e00\u884c\u65f6&#xff0c;\u5339\u914d\u6761\u4ef6&#xff0c;\u7136\u540e\u6267\u884c\u6307\u5b9a\u52a8\u4f5c&#xff0c;\u518d\u63a5\u7740\u8bfb\u53d6\u7b2c\u4e8c\u884c\u6570\u636e\u5904\u7406&#xff0c;\u4e0d\u4f1a\u9ed8\u8ba4\u8f93\u51fa\u3002\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\u5339\u914d\u6761\u4ef6\u9ed8\u8ba4\u662f\u5339\u914d\u6240\u6709\u6570\u636e\u884c&#xff0c;awk\u9690\u542b\u5faa\u73af&#xff0c;\u6761\u4ef6\u5339\u914d\u591a\u5c11\u6b21\u52a8\u4f5c\u5c31\u4f1a\u6267\u884c\u591a\u5c11\u6b21\u3002\u9010\u884c\u8bfb\u53d6\u6587\u672c&#xff0c;\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u6216tab\u952e\u4e3a\u5206\u9694\u7b26\u8fdb\u884c\u5206\u9694&#xff0c;\u5c06\u5206\u9694\u6240\u5f97\u7684\u5404\u4e2a\u5b57\u6bb5\u4fdd\u5b58\u5230\u5185\u5efa\u53d8\u91cf\u4e2d&#xff0c;\u5e76\u6309\u6a21\u5f0f\u6216\u8005\u6761\u4ef6\u6267\u884c\u7f16\u8f91\u547d\u4ee4\u3002sed\u547d\u4ee4\u5e38\u7528\u4e8e\u4e00\u6574\u884c\u7684\u5904\u7406&#xff0c;\u800cawk\u6bd4\u8f83\u3001\u503e\u5411\u4e8e\u5c06\u4e00\u884c\u5206\u6210\u591a\u4e2a&#034;\u5b57\u6bb5&#034;\u7136\u540e\u518d\u8fdb\u884c\u5904\u7406\u3002awk\u4fe1\u606f\u7684\u8bfb\u5165\u4e5f\u662f\u9010\u884c\u8bfb\u53d6\u7684&#xff0c;\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7print\u7684\u529f\u80fd\u5c06\u5b57\u6bb5\u6570\u636e\u6253\u5370\u663e\u793a\u3002\u5728\u4f7f\u7528awk\u547d\u4ee4\u7684\u8fc7\u7a0b\u4e2d,\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u64cd\u4f5c\u7b26&#034; &amp;&amp;\u201c\u8868\u793a&#034;\u4e0e\u201d\u3001\u201c||\u8868\u793a&#034;\u6216\u201d\u3001&#034;!\u201c\u8868\u793a\u975e\u201d;\u8fd8\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u6570\u5b66\u7684\u8fd0\u7b97&#xff0c;\u5982&#043;\u3001\u4e00\u3001\u3001\/\u3001%\u3001^\u5206\u522b\u8868\u793a\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3001\u53d6\u4f59\u548c\u4e58\u65b9\u3002<\/p>\n<h2>\u4e09\u3001\u5de5\u4f5c\u6d41\u7a0b<\/h2>\n<p>man gawk&#8212;-\u300bpattern scanning and processing language \u6a21\u5f0f\u626b\u63cf\u548c\u5904\u7406\u8bed\u8a00\u3002 pattern [\u02c8p\u00e6tn] \u6a21\u5f0f &#xff1b; process [\u02c8pr\u0259\u028ases] \u5904\u7406 \u4efb\u4f55 awk \u8bed\u53e5\u90fd\u662f\u7531\u6a21\u5f0f\u548c\u52a8\u4f5c\u7ec4\u6210&#xff0c;\u4e00\u4e2a awk \u811a\u672c\u53ef\u4ee5\u6709\u591a\u4e2a\u8bed\u53e5\u3002\u6a21\u5f0f\u51b3\u5b9a\u52a8\u4f5c\u8bed\u53e5\u7684\u89e6\u53d1\u6761\u4ef6\u548c\u89e6\u53d1\u65f6\u95f4\u3002\u5728 linux \u4e0a\u5e38\u7528\u7684\u662f gawk,awk \u662f gawk \u7684\u94fe\u63a5\u6587\u4ef6\u3002awk&#8212;-&gt;gawk \u5373&#xff1a; gun awk<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;benet22 opt<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># which awk<\/span><br \/>\n\/usr\/bin\/awk<br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;benet22 opt<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># ll which awk<\/span><br \/>\nlrwxrwxrwx. <span class=\"token number\">1<\/span> root root <span class=\"token number\">4<\/span> <span class=\"token number\">8<\/span>\u6708 <span class=\"token number\">19<\/span> <span class=\"token number\">2022<\/span> \/usr\/bin\/awk &#8211;<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">gawk<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b906d50d.png\" alt=\"image-20240725135619375\" \/><\/p>\n<h3>3.1 \u8fd0\u884c\u6a21\u5f0f<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f : \/root\/ \u5339\u914d\u542b\u6709 root \u7684\u884c \/.root\/ \u5173\u7cfb\u8868\u8fbe\u5f0f&#xff1a; &lt; &gt; &amp;&amp; || &#043; * \u5339\u914d\u8868\u8fbe\u5f0f&#xff1a; ~ &#xff01;~ \u52a8\u4f5c&#xff1a;\u53d8\u91cf \u547d\u4ee4 \u5185\u7f6e\u51fd\u6570 \u6d41\u63a7\u5236\u8bed\u53e5\u5b83\u7684\u8bed\u6cd5\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<h3>3.2 \u6267\u884c\u6d41\u7a0b<\/h3>\n<p>BEGIN \u8bed\u53e5\u8bbe\u7f6e\u8ba1\u6570\u548c\u6253\u5370\u5934\u90e8\u4fe1\u606f&#xff0c;\u5728\u4efb\u4f55\u52a8\u4f5c\u4e4b\u524d\u8fdb\u884c\u3002END \u8bed\u53e5\u8f93\u51fa\u7edf\u8ba1\u7ed3\u679c&#xff0c;\u5728\u5b8c\u6210\u52a8\u4f5c\u4e4b\u540e\u6267\u884c\u3002AWK\u6267\u884c\u7684\u6d41\u7a0b\u975e\u5e38\u7b80\u5355&#xff1a;\u8bfb&#xff08;Read&#xff09;\u3001\u6267\u884c&#xff08;Execute&#xff09;\u4e0e\u91cd\u590d&#xff08;Repeat&#xff09;\u3002\u4e0b\u9762\u7684\u6d41\u7a0b\u56fe\u63cf\u8ff0\u51fa\u4e86AWK\u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<p><span class=\"token function\">awk<\/span> <span class=\"token punctuation\">[<\/span>options<span class=\"token punctuation\">]<\/span> <span class=\"token string\">&#039;BEGIN{ print &#034;start&#034; } \u2018pattern{ commands }\u2019 END{ print &#034;end&#034; }&#039;<\/span> <span class=\"token function\">file<\/span><\/p>\n<p>\u5176\u4e2d&#xff1a;BEGIN END \u662f AWK \u7684\u5173\u952e\u5b57\u90e8&#xff0c;\u56e0\u6b64\u5fc5\u987b\u5927\u5199&#xff1b;\u8fd9\u4e24\u4e2a\u90e8\u5206\u5f00\u59cb\u5757\u548c\u7ed3\u675f\u5757\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>\u4ece\u4e0a\u56fe\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86AWK\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u7a0b\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u4e00\u770bAWK\u7a0b\u5e8f\u7684\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49&#xff0c;\u5f00\u59cb\u5757\u5c31\u662f\u5728\u7a0b\u5e8f\u542f\u52a8\u7684\u65f6\u5019\u6267\u884c\u7684\u4ee3\u7801\u90e8\u5206&#xff0c;\u5e76\u4e14\u5b83\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u53ea\u6267\u884c\u4e00\u6b21\u3002\u4e00\u822c\u60c5\u51b5\u4e0b&#xff0c;\u6211\u4eec\u5728\u5f00\u59cb\u5757\u4e2d\u521d\u59cb\u5316\u4e00\u4e9b\u53d8\u91cf\u3002BEGIN\u662fAWK\u7684\u5173\u952e\u5b57&#xff0c;\u56e0\u6b64\u5b83\u5fc5\u987b\u662f\u5927\u5199\u7684\u3002\u4e0d\u8fc7&#xff0c;\u5f00\u59cb\u5757\u90e8\u5206\u662f\u53ef\u9009\u7684&#xff0c;\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u6ca1\u6709\u5f00\u59cb\u5757\u90e8\u5206\u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u8f93\u5165\u7684\u884c&#xff0c;\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\u4e3b\u4f53\u90e8\u5206\u7684\u547d\u4ee4\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u5bf9\u4e8e\u8f93\u5165\u7684\u6bcf\u4e00\u884c&#xff0c;AWK\u90fd\u4f1a\u6267\u884c\u547d\u4ee4\u3002\u6ce8\u610f&#xff1a;\u5728\u4e3b\u4f53\u5757\u90e8\u5206&#xff0c;\u6ca1\u6709\u5173\u952e\u5b57\u5b58\u5728\u3002\u5b83\u662f\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u6267\u884c\u7684\u4ee3\u7801\u3002END\u4e5f\u662fAWK\u7684\u5173\u952e\u5b57&#xff0c;\u5b83\u4e5f\u5fc5\u987b\u5927\u5199\u3002\u4e0e\u5f00\u59cb\u5757\u76f8\u4f3c&#xff0c;\u7ed3\u675f\u5757\u4e5f\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u6211\u4eec\u53ef\u4ee5\u77e5\u9053AWK \u5b83\u5de5\u4f5c\u901a\u8fc7\u4e09\u4e2a\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u8bfb&#xff08;Read&#xff09;&#xff1a;\u4ece\u6587\u4ef6\u3001\u7ba1\u9053\u6216\u6807\u51c6\u8f93\u5165\u4e2d\u8bfb\u5165\u4e00\u884c\u7136\u540e\u628a\u5b83\u5b58\u653e\u5230\u5185\u5b58\u4e2d<\/li>\n<li>\u6267\u884c&#xff08;Execute&#xff09;&#xff1a;\u5bf9\u6bcf\u4e00\u884c\u6570\u636e&#xff0c;\u6839\u636e AWK \u547d\u4ee4\u6309\u987a\u5e8f\u6267\u884c\u3002\u9ed8\u8ba4\u60c5\u51b5\u662f\u5904\u7406\u6bcf\u4e00\u884c\u6570\u636e&#xff0c;\u4e5f\u53ef\u4ee5\u6307\u5b9a\u6a21\u5f0f<\/li>\n<li>\u91cd\u590d&#xff08;Repeat&#xff09;&#xff1a;\u4e00\u76f4\u91cd\u590d\u4e0a\u8ff0\u4e24\u4e2a\u8fc7\u7a0b\u76f4\u5230\u6587\u4ef6\u7ed3\u675f<\/li>\n<h2>\u56db\u3001\u57fa\u672c\u8bed\u6cd5<\/h2>\n<h3>4.1 \u547d\u4ee4\u683c\u5f0f<\/h3>\n<p><span class=\"token function\">awk<\/span> \u9009\u9879<span class=\"token string\">&#039; \u6a21\u5f0f\u6216\u6761\u4ef6{\u64cd\u4f5c}&#039;<\/span> \u6587\u4ef61 \u6587\u4ef62 <span class=\"token punctuation\">..<\/span>.<br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token parameter variable\">-f<\/span> \u811a\u672c\u6587\u4ef6 \u6587\u4ef61 \u6587\u4ef62 <span class=\"token punctuation\">..<\/span><\/p>\n<p>\u683c\u5f0f&#xff1a;awk\u5173\u952e\u5b57 \u9009\u9879 \u547d\u4ee4\u90e8\u5206 \u2018{xxxx}\u2019 \u6587\u4ef6\u540d<\/p>\n<p>AWK \u652f\u6301\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf&#xff1a;\u5185\u5efa\u53d8\u91cf(\u53ef\u76f4\u63a5\u4f7f\u7528)&#xff0c;\u81ea\u5b9a\u4e49\u53d8\u91cfawk \u5185\u7f6e\u53d8\u91cf&#xff08;\u9884\u5b9a\u4e49\u53d8\u91cf&#xff09;\u5982\u4e0b\u6240\u793a&#xff1a;<\/p>\n<ul>\n<li>FS&#xff1a;\u6307\u5b9a\u6bcf\u884c\u6587\u672c\u7684\u5b57\u6bb5\u5206\u9694\u7b26&#xff0c;\u9ed8\u8ba4\u4e3a\u7a7a\u683c\u6216\u5236\u8868\u4f4d\u3002<\/li>\n<li>NF&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u5b57\u6bb5\u4e2a\u6570\u3002\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5e94\u4e8e\u5f53\u524d\u7684\u5b57\u6bb5\u6570&#xff0c;NF&#xff1a;\u5217\u7684\u4e2a\u6570<\/li>\n<li>NR&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u884c\u53f7&#xff08;\u5e8f\u6570&#xff09;\u3002 \u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5e94\u4e8e\u5f53\u524d\u7684\u884c\u53f7<\/li>\n<li>$0&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u6574\u884c\u5185\u5bb9\u3002<\/li>\n<li>$n&#xff1a;\u5f53\u524d\u5904\u7406\u884c\u7684\u7b2c n \u4e2a\u5b57\u6bb5&#xff08;\u7b2c n \u5217&#xff09;\u3002\u6bd4\u5982: $1 \u8868\u793a\u7b2c\u4e00\u4e2a\u5b57\u6bb5&#xff0c;$2 \u8868\u793a\u7b2c\u4e8c\u4e2a\u5b57\u6bb5<\/li>\n<li>FILENAME&#xff1a;\u88ab\u5904\u7406\u7684\u6587\u4ef6\u540d(\u5f53\u524d\u8f93\u5165\u6587\u4ef6\u7684\u540d)\u3002<\/li>\n<li>FNR \u5404\u6587\u4ef6\u5206\u522b\u8ba1\u6570\u7684\u884c\u53f7<\/li>\n<li>OFS \u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26&#xff08;\u9ed8\u8ba4\u503c\u662f\u4e00\u4e2a\u7a7a\u683c&#xff09;<\/li>\n<li>ORS \u8f93\u51fa\u8bb0\u5f55\u5206\u9694\u7b26&#xff08;\u9ed8\u8ba4\u503c\u662f\u4e00\u4e2a\u6362\u884c\u7b26&#xff09;<\/li>\n<li>RS:\u884c\u5206\u9694\u7b26\u3002awk\u4ece\u6587\u4ef6\u4e0a\u8bfb\u53d6\u8d44\u6599\u65f6,\u5c06\u6839\u636eRs\u7684\u5b9a\u4e49\u628a\u8d44\u6599\u5207\u5272\u6210\u8bb8\u591a\u6761\u8bb0\u5f55, \u800cawk\u4e00\u6b21\u4ec5\u8bfb\u5165\u4e00\u6761\u8bb0\u5f55,\u4ee5\u8fdb\u884c\u5904\u7406\u3002\u9884\u8bbe\u503c\u662f&#034; \\\\n\u2019<\/li>\n<\/ul>\n<p>\u7b80\u8bf4&#xff1a;\u6570\u636e\u8bb0\u5f55\u5206\u9694&#xff0c;\u9ed8\u8ba4\u4e3a\\\\n&#xff0c;\u5373\u6bcf\u884c\u4e3a\u4e00\u6761\u8bb0\u5f55\u3002<\/p>\n<h2>\u4e94\u3001\u5b9e\u6218\u6848\u4f8b<\/h2>\n<h3>5.1 \u5f00\u59cb\u5757&#xff08;BEGIN block&#xff09;<\/h3>\n<p>\u4e3b\u4f53\u5757&#xff08;Body Block&#xff09;\u7ed3\u675f\u5757&#xff08;END Block&#xff09;<\/p>\n<p>\u5185\u5efa\u53d8\u91cf<\/p>\n<p>awk \u5305\u542b\u51e0\u4e2a\u7279\u6b8a\u7684\u5185\u5efa\u53d8\u91cf&#xff08;\u53ef\u76f4\u63a5\u7528&#xff09;\u5982\u4e0b\u6240\u793a&#xff1a;<\/p>\n<ul>\n<li>FS&#xff1a;\u6307\u5b9a\u6bcf\u884c\u6587\u672c\u7684\u5b57\u6bb5\u5206\u9694\u7b26&#xff0c;\u9ed8\u8ba4\u4e3a\u7a7a\u683c\u6216\u5236\u8868\u4f4d\u3002<\/li>\n<li>NF&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u5b57\u6bb5\u4e2a\u6570\u3002<\/li>\n<li>NR&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u884c\u53f7&#xff08;\u5e8f\u6570&#xff09;\u3002<\/li>\n<li>$0&#xff1a;\u5f53\u524d\u5904\u7406\u7684\u884c\u7684\u6574\u884c\u5185\u5bb9\u3002<\/li>\n<li>$n&#xff1a;\u5f53\u524d\u5904\u7406\u884c\u7684\u7b2c n \u4e2a\u5b57\u6bb5&#xff08;\u7b2c n \u5217&#xff09;\u3002<\/li>\n<li>FILENAME&#xff1a;\u88ab\u5904\u7406\u7684\u6587\u4ef6\u540d\u3002<\/li>\n<li>RS:\u884c\u5206\u9694\u7b26\u3002awk\u4ece\u6587\u4ef6\u4e0a\u8bfb\u53d6\u8d44\u6599\u65f6,\u5c06\u6839\u636eRs\u7684\u5b9a\u4e49\u628a\u8d44\u6599\u5207\u5272\u6210\u8bb8\u591a\u6761\u8bb0\u5f55, \u800cawk\u4e00\u6b21\u4ec5\u8bfb\u5165\u4e00\u6761\u8bb0\u5f55,\u4ee5\u8fdb\u884c\u5904\u7406\u3002\u9884\u8bbe\u503c\u662f&#034; \\\\n\u2019<\/li>\n<\/ul>\n<p>\u7b80\u8bf4&#xff1a;\u6570\u636e\u8bb0\u5f55\u5206\u9694&#xff0c;\u9ed8\u8ba4\u4e3a\\\\n&#xff0c;\u5373\u6bcf\u884c\u4e3a\u4e00\u6761\u8bb0\u5f55<\/p>\n<h4>5.1.1print<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;patterm{action}&#039;<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039; &#039;<\/span><\/p>\n<p>\u4ec0\u4e48\u90fd\u4e0d\u5199 \u7a7a\u6ca1\u6709\u6548\u679c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b9081e1c.png\" alt=\"image-20240725135951912\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;{print}&#039;<\/span><\/p>\n<p>\u5728\u6253\u5370\u4e00\u904d<\/p>\n<p>dd<br \/>\ndd<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b908bcf8.png\" alt=\"image-20240725140039786\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;{print &#034;hello&#034;}&#039;<\/span><\/p>\n<p>\u5b57\u7b26\u4e32\u9700\u8981\u6dfb\u52a0\u53cc\u5f15\u53f7&#xff0c;\u5355\u5f15\u53f7\u5df2\u88ab\u4f7f\u7528<\/p>\n<p>1<br \/>\nhello<br \/>\n1<br \/>\nhello<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b909c4c3.png\" alt=\"image-20240725140150442\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;{print &#034;hello&#034;}&#039; &lt; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># cat \/etc\/passwd | head -10 &gt; zz<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print}&#039; zz<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b90ab9dd.png\" alt=\"image-20240725140402511\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print $1}&#039; zz<\/span><\/p>\n<p>root:x:0:0:root:\/root:\/bin\/bash<\/p>\n<p>awk\u9ed8\u8ba4\u628a\u8fd9\u4e00\u884c\u5f53\u505a\u4e00\u5217&#xff0c;\u56e0\u4e3a\u6ca1\u6709\u88ab\u7a7a\u683c\u5206\u9694&#xff0c;awk\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u6216tab\u952e\u5206\u9694<\/p>\n<p>bin:x:1:1:bin:\/bin:\/sbin\/nologin<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b90d568d.png\" alt=\"image-20240725145313469\" \/><\/p>\n<h3>5.2 \u5185\u7f6e\u53d8\u91cf<\/h3>\n<p>awk\u5e38\u7528\u5185\u7f6e\u53d8\u91cf&#xff1a;$1\u3001$2<\/p>\n<p>\u3001NF\u3001NR\u3001$0<\/p>\n<ul>\n<li>$1&#xff1a;\u4ee3\u8868\u7b2c\u4e00\u5217<\/li>\n<li>$2&#xff1a;\u4ee3\u8868\u7b2c\u4e8c\u5217\u4ee5\u6b64\u7c7b\u63a8<\/li>\n<li>$0&#xff1a;\u4ee3\u8868\u6574\u884c<\/li>\n<li>NF&#xff1a;\u4e00\u884c\u7684\u5217\u6570<\/li>\n<li>NR&#xff1a;\u884c\u6570<\/li>\n<\/ul>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print $5}&#039; zz<\/span><\/p>\n<p>\u81ea\u5b9a\u4e49\u5192\u53f7\u4e3a\u5206\u9694\u7b26\u663e\u793a\u5206\u9694\u4e4b\u540e\u7684\u7b2c\u4e94\u5217<\/p>\n<p>root<br \/>\nbin<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -Fx &#039;{print $1}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u7528x\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n<p>root:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b910787c.png\" alt=\"image-20240725145529230\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print $1 $2}&#039; zz<\/span><\/p>\n<p>helloworld<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print $1&#034;&#034;$2}&#039; zz<\/span><\/p>\n<p>\u663e\u793a\u4e00\u4e2a\u7a7a\u683c&#xff0c;\u7a7a\u683c\u9700\u8981\u7528\u53cc\u5f15\u53f7\u5f15\u8d77\u6765&#xff0c;\u5982\u679c\u4e0d\u7528\u5f15\u53f7\u9ed8\u8ba4\u4ee5\u53d8\u91cf\u770b\u5f85&#xff0c;\u5982\u679c\u662f\u5e38\u91cf\u5c31\u9700\u8981\u53cc\u5f15\u53f7\u5f15\u8d77\u6765<\/p>\n<p>hello world<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print $1,$2}&#039; zz<\/span><\/p>\n<p>\u9017\u53f7\u6709\u7a7a\u683c\u6548\u679c<\/p>\n<p>hello world<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b912d2a7.png\" alt=\"image-20240725145814677\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print $1&#034;\\\\t&#034;$2}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u7528\u5236\u8868\u7b26\u4f5c\u4e3a\u5206\u9694\u7b26\u8f93\u51fa<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F[:\/] &#039;{print $9}&#039; zz<\/span><\/p>\n<p>\u5b9a\u4e49\u591a\u4e2a\u5206\u9694\u7b26&#xff0c;\u53ea\u8981\u770b\u5230\u5176\u4e2d\u4e00\u4e2a\u90fd\u7b97\u4f5c\u5206\u9694\u7b26<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b913868e.png\" alt=\"image-20240725150112484\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;\/root\/{print $0}&#039; pass.txt<\/span><\/p>\n<p>\u6253\u5370\u5305\u542broot\u7684\u6574\u884c\u5185\u5bb9<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;\/root\/{print $1}&#039; pass.txt<\/span><\/p>\n<p>\u6253\u5370\u5305\u542broot\u7684\u884c\u7684\u7b2c\u4e00\u5217<\/p>\n<p>root<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;\/root\/{print $1,$6}&#039; pass.txt<\/span><\/p>\n<p>\u6253\u5370\u5305\u542broot\u7684\u884c\u7684\u7b2c\u4e00\u5217\u548c\u7b2c\u516d\u5217<\/p>\n<p>root \/root<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;\/root\/&#039; \/etc\/passwd<\/span><\/p>\n<p>root:x:0:0:root:\/root:\/bin\/bash<br \/>\noperator:x:11:0:operator:\/root:\/sbin\/nologin<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b915b049.png\" alt=\"image-20240725150616173\" \/><\/p>\n<p>\u8f93\u51fashadow\u6587\u4ef6\u4e2d\u4ee5\u5192\u53f7\u5206\u9694\u7684\u7b2c\u4e8c\u5217\u6709\u611f\u53f9\u53f7\u7684\u884c\u7684\u7b2c\u4e00\u5217<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b917cbc0.png\" alt=\"image-20240725151057078\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F[:\/] &#039;{print NF}&#039; zz<\/span><\/p>\n<p>\u6253\u5370\u6bcf\u4e00\u884c\u7684\u5217\u6570<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F[:\/] &#039;{print NR}&#039; zz<\/span><\/p>\n<p>\u663e\u793a\u884c\u53f7<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b9195179.png\" alt=\"image-20240725151348884\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print NR}&#039; pass.txt<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print NR,$0}&#039; pass.txt<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;NR&#061;&#061;2&#039; \/etc\/passwd<\/span><\/p>\n<p>\u6253\u5370\u7b2c\u4e8c\u884c&#xff0c;\u4e0d\u52a0print\u4e5f\u4e00\u6837&#xff0c;\u9ed8\u8ba4\u5c31\u662f\u6253\u5370<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;NR&#061;&#061;2{print}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b91ba61c.png\" alt=\"image-20240725152221839\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;NR&#061;&#061;2{print $1}&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print $NF}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u6253\u5370\u6700\u540e\u4e00\u5217<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;END{print NR}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u6253\u5370\u603b\u884c\u6570<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;END{print $0}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u6253\u5370\u6587\u4ef6\u6700\u540e\u4e00\u884c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092953-68021b91e505d.png\" alt=\"image-20240725152709276\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print &#034;\u5f53\u524d\u884c\u6709&#034;NF&#034;\u5217&#034;}&#039; zz<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{print &#034;\u7b2c&#034;NR&#034;\u884c\u6709&#034;NF&#034;\u5217&#034;}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u7b2c\u51e0\u884c\u6709\u51e0\u5217<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b920fabc.png\" alt=\"image-20240725153213203\" \/><\/p>\n<p>\u6269\u5c55\u751f\u4ea7\u6848\u4f8b&#xff1a;\u7f51\u5361\u7684ip\u3001\u6d41\u91cf<\/p>\n<h3>5.3 BEGIN END \u8fd0\u7b97<\/h3>\n<p>\u9010\u884c\u6267\u884c\u5f00\u59cb\u4e4b\u524d\u6267\u884c\u4ec0\u4e48\u4efb\u52a1&#xff0c;\u7ed3\u675f\u4e4b\u540e\u518d\u6267\u884c\u4ec0\u4e48\u4efb\u52a1&#xff0c;\u7528BEGIN\u3001END\u3002BEGIN\u4e00\u822c\u7528\u6765\u505a\u521d\u59cb\u5316\u64cd\u4f5c&#xff0c;\u4ec5\u5728\u8bfb\u53d6\u6570\u636e\u8bb0\u5f55\u4e4b\u524d\u6267\u884c\u4e00\u6b21\u3002END\u4e00\u822c\u7528\u6765\u505a\u6c47\u603b\u64cd\u4f5c&#xff0c;\u4ec5\u5728\u8bfb\u53d6\u5b8c\u6570\u636e\u8bb0\u5f55\u4e4b\u540e\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<h4>5.3.1 \u6269\u5c55\u751f\u4ea7&#xff1a;\u7f51\u5361\u7684ip\u3001\u6d41\u91cf<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># ifconfig ens33 | awk &#039;\/netmask\/{print &#034;\u672c\u673a\u7684ip\u5730\u5740\u662f&#034;$2}&#039;<\/span><\/p>\n<p>$\u672c\u673a\u7684ip\u5730\u5740\u662f192.168.245.211<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># ifconfig ens33 | awk &#039;\/RX p\/{print $5&#034;\u5b57\u8282&#034;}&#039;<\/span><\/p>\n<p>8341053\u5b57\u8282<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b9226aab.png\" alt=\"image-20240725160006321\" \/><\/p>\n<h4>5.3.2 \u6839\u5206\u533a\u7684\u53ef\u7528\u91cf<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># df -h | awk &#039;NR&#061;&#061;2{print $4}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b923c131.png\" alt=\"image-20240725160340610\" \/><\/p>\n<h4>5.3.3 awk\u7684\u8fd0\u7b97&#xff1a;<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{x&#061;10;print x}&#039;<\/span><\/p>\n<p>\u5982\u679c\u4e0d\u7528\u5f15\u53f7awk\u5c31\u5f53\u4f5c\u4e00\u4e2a\u53d8\u91cf\u6765\u8f93\u51fa\u4e86&#xff0c;\u6240\u4ee5\u4e0d\u9700\u8981\u52a0$\u4e86\u3002<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{x&#061;10;print x&#043;1}&#039;<\/span><\/p>\n<p>BEGIN\u5728\u5904\u7406\u6587\u4ef6\u4e4b\u524d&#xff0c;\u6240\u4ee5\u540e\u9762\u4e0d\u8ddf\u6587\u4ef6\u540d\u4e5f\u4e0d\u5f71\u54cd<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{x&#061;10;x&#043;&#043;;print x}&#039;<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print x&#043;1}&#039;<\/span><\/p>\n<p>\u4e0d\u6307\u5b9a\u521d\u59cb\u503c&#xff0c;\u521d\u59cb\u503c\u5c31\u4e3a0&#xff0c;\u5982\u679c\u662f\u5b57\u7b26\u4e32&#xff0c;\u5219\u9ed8\u8ba4\u4e3a\u7a7a<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 2.5&#043;3.5}&#039;<\/span><\/p>\n<p>\u5c0f\u6570\u4e5f\u53ef\u4ee5\u8fd0\u7b97<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 2-1}&#039;<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 3*4}&#039;<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 32}&#039;<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 2^3}&#039;<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print 1\/2}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b9247bc3.png\" alt=\"image-20240725161242696\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;\/root\/&#039; \/etc\/passwd<\/span><\/p>\n<p>\u5982\u679c\u540e\u9762\u6709\u5177\u4f53\u6253\u5370\u591a\u5c11\u5217\u5c31\u6ca1\u6cd5\u7701\u7565print\u4e86<\/p>\n<p>\u6a21\u7cca\u5339\u914d&#xff0c;\u7528\u8868\u793a\u5305\u542b&#xff0c;!~\u8868\u793a\u4e0d\u5305\u542b<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$1~\/root\/&#039; \/etc\/passwd<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$1~\/ro\/&#039; \/etc\/passwd<\/span><\/p>\n<p>\u6a21\u7cca\u5339\u914d&#xff0c;\u53ea\u8981\u6709ro\u5c31\u5339\u914d\u4e0a<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$7!~\/nologin$\/{print $1,$7}&#039; \/etc\/pass<\/span><\/p>\n<p>wd<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b926b5f9.png\" alt=\"image-20240725161842207\" \/><\/p>\n<p>\u5173\u4e8e\u6570\u503c\u4e0e\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83 \u6bd4\u8f83\u7b26\u53f7&#xff1a;&#061;&#061; !&#061; &lt;&#061; &gt;&#061; &lt; &gt;<\/p>\n<h4>5.3.4\u6848\u4f8b<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;NR&#061;&#061;5{print}&#039; \/etc\/passwd<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;NR&#061;&#061;5&#039; \/etc\/passwd<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;NR&lt;5&#039; \/etc\/passwd<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$3&#061;&#061;0&#039; \/etc\/passwd<\/span><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$1&#061;&#061;root&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$1&#061;&#061;&#034;root&#034;&#039; \/etc\/passwd<\/span><\/p>\n<p>\u7cbe\u786e\u5339\u914d\u4e00\u5b9a\u662froot<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092954-68021b92956fc.png\" alt=\"image-20240725162406327\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$3&gt;&#061;1000&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-182x1ycrpcoh3.png\" alt=\"image-20240725162713344\" \/><\/p>\n<p>\u903b\u8f91\u8fd0\u7b97 &amp;&amp; || &amp;&amp; \u548c || \u662f\u903b\u8f91\u8fd0\u7b97\u7b26&#xff0c;\u7528\u4e8e\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u5e76\u63a7\u5236\u7a0b\u5e8f\u6d41\u7a0b\u3002 &amp;&amp; \u8981\u6c42\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u771f\u65f6\u624d\u4e3a\u771f&#xff0c;\u5426\u5219\u4e3a\u5047\u3002 || \u53ea\u8981\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\u5c31\u4e3a\u771f&#xff0c;\u5168\u4e3a\u5047\u65f6\u624d\u4e3a\u5047\u3002<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$3&lt;10 || $3&gt;&#061;1000&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;$3&gt;10 &amp;&amp; $3&lt;1000&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;NR&gt;4 &amp;&amp; NR&lt;10&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18qrj4vnsie5u.png\" alt=\"image-20240725162944359\" \/><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18venpnnsrpt1.png\" alt=\"image-20240725163046086\" \/><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18mf3cxk1ovyh.png\" alt=\"image-20240725163150951\" \/><\/p>\n<h4>5.3.5 \u6848\u4f8b<\/h4>\n<p>\u6253\u53701-200\u4e4b\u95f4\u6240\u6709\u80fd\u88ab7\u6574\u9664\u5e76\u4e14\u5305\u542b\u6570\u5b577\u7684\u6574\u6570\u6570\u5b57<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\"># seq 200 | awk &#039;$1%7&#061;&#061;0 &amp;&amp; $1~\/7\/&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18c4fly3ar2n4.png\" alt=\"image-20240725163812495\" \/><\/p>\n<p>\u5176\u4ed6\u5185\u7f6e\u53d8\u91cf\u7684\u7528\u6cd5FS&#xff08;\u8f93\u5165&#xff09;\u3001OFS\u3001NR\u3001FNR\u3001RS\u3001ORS<\/p>\n<ul>\n<li>FS&#xff1a;\u8f93\u5165\u5b57\u6bb5\u7684\u5206\u9694\u7b26 \u9ed8\u8ba4\u662f\u7a7a\u683c<\/li>\n<li>OFS&#xff1a;\u8f93\u51fa\u5b57\u6bb5\u7684\u5206\u9694\u7b26 \u9ed8\u8ba4\u4e5f\u662f\u7a7a\u683c<\/li>\n<li>FNR&#xff1a;\u8bfb\u53d6\u6587\u4ef6\u7684\u8bb0\u5f55\u6570&#xff08;\u884c\u53f7&#xff09;&#xff0c;\u4ece1\u5f00\u59cb&#xff0c;\u65b0\u7684\u6587\u4ef6\u91cd\u65b0\u91cd1\u5f00\u59cb\u8ba1\u6570<\/li>\n<li>RS&#xff1a;\u8f93\u5165\u884c\u5206\u9694\u7b26 \u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26<\/li>\n<li>ORS&#xff1a;\u8f93\u51fa\u884c\u5206\u9694\u7b26 \u9ed8\u8ba4\u4e5f\u662f\u4e3a\u6362\u884c\u7b26<\/li>\n<\/ul>\n<h4>5.3.6 FS<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{FS&#061;&#034;:&#034;}{print $1}&#039; pass.txt<\/span><\/p>\n<p>\u5728\u6253\u5370\u4e4b\u524d\u5b9a\u4e49\u5b57\u6bb5\u5206\u9694\u7b26\u4e3a\u5192\u53f7<\/p>\n<h4>5.3.7 OFS<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{FS&#061;&#034;:&#034;;OFS&#061;&#034;&#8212;&#034;}{print $1,$2}&#039; pass.txt<\/span><\/p>\n<p>OFS\u5b9a\u4e49\u4e86\u8f93\u51fa\u65f6\u4ee5\u4ec0\u4e48\u5206\u9694&#xff0c;$1$2\u4e2d\u95f4\u8981\u7528\u9017\u53f7\u5206\u9694&#xff0c;\u56e0\u4e3a\u9017\u53f7\u9ed8\u8ba4\u88ab\u6620\u5c04\u4e3aOFS\u53d8\u91cf&#xff0c;\u800c\u8fd9\u4e2a\u53d8\u91cf\u9ed8\u8ba4\u662f\u7a7a\u683c<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18xsxgyugupc5.png\" alt=\"image-20240725170613271\" \/><\/p>\n<h4>5.3.8 FNR<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print FNR,$0}&#039; \/etc\/resolv.conf \/etc\/hosts<\/span><\/p>\n<p>\u53ef\u4ee5\u770b\u51faFNR\u7684\u884c\u53f7\u5728\u8ffd\u52a0\u5f53\u6709\u591a\u4e2a\u6587\u4ef6\u65f6<\/p>\n<h4>5.3.9 NR<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;{print NR,$0}&#039; \/etc\/resolv.conf \/etc\/hosts<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18rckmfk5k0ur.png\" alt=\"image-20240725171352653\" \/><\/p>\n<h4>5.3.10 RS<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{RS&#061;&#034;:&#034;}{print $0}&#039; \/etc\/passwd<\/span><\/p>\n<p>RS:\u6307\u5b9a\u4ee5\u4ec0\u4e48\u4e3a\u6362\u884c\u7b26&#xff0c;\u8fd9\u91cc\u6307\u5b9a\u662f\u5192\u53f7&#xff0c;\u4f60\u6307\u5b9a\u7684\u80af\u5b9a\u662f\u539f\u6587\u91cc\u5b58\u5728\u7684\u5b57\u7b26<\/p>\n<p><img decoding=\"async\" src=\"2025-04-185sghccv2k4b.png\" alt=\"\" \/><\/p>\n<h4>5.3.11 ORS<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{ORS&#061;&#034; &#034;}{print $0}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u628a\u591a\u884c\u5408\u5e76\u6210\u4e00\u884c\u8f93\u51fa&#xff0c;\u8f93\u51fa\u7684\u65f6\u5019\u81ea\u5b9a\u4e49\u4ee5\u7a7a\u683c\u5206\u9694\u6bcf\u884c&#xff0c;\u672c\u6765\u9ed8\u8ba4\u7684\u662f\u56de\u8f66\u952e<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18wdxkcihgqew.png\" alt=\"image-20240725172813030\" \/><\/p>\n<h3>5.4 \u5f15\u7528\u53d8\u91cf<\/h3>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># a&#061;100<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -v b&#061;&#034;$a&#034; &#039;BEGIN{print b}&#039;<\/span><\/p>\n<p>\u5c06\u7cfb\u7edf\u7684\u53d8\u91cfa&#xff0c;\u5728awk\u91cc\u8d4b\u503c\u4e3a\u53d8\u91cfb&#xff0c;\u7136\u540e\u8c03\u7528\u53d8\u91cfb -v \u9009\u9879\u5c06\u5176\u4f20\u9012\u7ed9 awk<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk &#039;BEGIN{print &#034;&#039;$a&#039;&#034;}&#039;<\/span><\/p>\n<p>\u76f4\u63a5\u8c03\u7528\u7684\u8bdd\u9700\u8981\u5148\u7528\u53cc\u5f15\u53f7\u518d\u7528\u5355\u5f15\u53f7<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18ehimfnbbwgf.png\" alt=\"image-20240811023859162\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -vc&#061;1 &#039;BEGIN{print c}&#039;<\/span><\/p>\n<p>awk\u76f4\u63a5\u5b9a\u4e49\u53d8\u91cf\u5e76\u5f15\u7528<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># df -h | awk &#039;BEGIN{getline}\/root\/{print $0}&#039;<\/span><\/p>\n<p>\u8c03\u7528\u51fd\u6570getline&#xff0c;\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u7684\u65f6\u5019\u5e76\u4e0d\u662f\u5f97\u5230\u5f53\u524d\u884c\u800c\u662f\u5f53\u524d\u884c\u7684\u4e0b\u4e00\u884c<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18bbpsknoyjw3.png\" alt=\"image-20240811024100245\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># seq 10 | awk &#039;{getline;print $0}&#039;<\/span><\/p>\n<p>\u663e\u793a\u5076\u6570\u884c<\/p>\n<p>2<br \/>\n4<br \/>\n6<br \/>\n8<br \/>\n10<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18yilmtnudbia.png\" alt=\"image-20240811024242503\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># seq 10 | awk &#039;{print $0;getline}&#039;<\/span><\/p>\n<p>\u663e\u793a\u5947\u6570\u884c<\/p>\n<p>1<br \/>\n3<br \/>\n5<br \/>\n7<br \/>\n9<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18pjriqedvzeh.png\" alt=\"image-20240811024332037\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{if($3&lt;10){print $0}}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18uae5swpqjck.png\" alt=\"image-20240811024456975\" \/><\/p>\n<p>\u7b2c\u4e09\u5217\u5c0f\u4e8e10\u7684\u6253\u5370\u6574\u884c<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\"># awk -F: &#039;{if($3&lt;10){print $3}else{print $1}}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18sr1q55b0i04.png\" alt=\"image-20240811024612888\" \/><\/p>\n<p>\u7b2c\u4e09\u5217\u5c0f\u4e8e10\u7684\u6253\u5370\u7b2c\u4e09\u5217&#xff0c;\u5426\u5219\u6253\u5370\u7b2c\u4e00\u5217<\/p>\n<p>\u7b2c\u4e8c\u6b65&#xff1a;\u4ece\u6587\u4ef6\u6216\u6807\u51c6\u8f93\u5165(stdin)\u8bfb\u53d6\u4e00\u884c\u3002\u7136\u540e\u8fd0\u884cpattern{ commands }\u8bed\u53e5\u5757&#xff0c;\u5b83\u9010\u884c\u626b\u63cf\u6587\u4ef6&#xff0c;\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u53cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u76f4\u5230\u6587\u4ef6\u6240\u6709\u88ab\u8bfb\u53d6\u5b8c\u6210\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff1a;\u5f53\u8bfb\u81f3\u8f93\u5165\u6d41\u672b\u5c3e\u65f6&#xff0c;\u8fd0\u884cEND{ commands }\u8bed\u53e5\u5757\u3002BEGIN\u8bed\u53e5\u5757\u5728awk\u5f00\u59cb\u4ece\u8f93\u5165\u6d41\u4e2d\u8bfb\u53d6\u884c\u4e4b\u524d\u88ab\u8fd0\u884c&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u8bed\u53e5\u5757&#xff0c;\u6bd4\u65b9\u53d8\u91cf\u521d\u59cb\u5316\u3001\u6253\u5370\u8f93\u51fa\u8868\u683c\u7684\u8868\u5934\u7b49\u8bed\u53e5\u901a\u5e38\u80fd\u591f\u5199\u5728BEGIN\u8bed\u53e5\u5757\u4e2d\u3002END\u8bed\u53e5\u5757\u5728awk\u4ece\u8f93\u5165\u6d41\u4e2d\u8bfb\u53d6\u5168\u7136\u90e8\u7684\u884c\u4e4b\u540e\u5373\u88ab\u8fd0\u884c\u3002\u6bd4\u65b9\u6253\u5370\u5168\u90e8\u884c\u7684\u5206\u6790\u7ed3\u679c\u8fd9\u7c7b\u4fe1\u606f\u6c47\u603b\u90fd\u662f\u5728END\u8bed\u53e5\u5757\u4e2d\u5b8c\u6bd5&#xff0c;\u5b83\u4e5f\u662f\u4e00\u4e2a\u53ef\u9009\u8bed\u53e5\u5757\u3002pattern\u8bed\u53e5\u5757\u4e2d\u7684\u901a\u7528\u547d\u4ee4\u662f\u6700\u91cd\u8981\u7684\u90e8\u5206&#xff0c;\u5b83\u4e5f\u662f\u53ef\u9009\u7684\u3002\u5047\u8bbe\u6ca1\u6709\u63d0\u4f9bpattern\u8bed\u53e5\u5757&#xff0c;\u5219\u9ed8\u8ba4\u8fd0\u884c{ print }&#xff0c;\u5373\u6253\u5370\u6bcf\u4e2a\u8bfb\u53d6\u5230\u7684\u884c\u3002awk\u8bfb\u53d6\u7684\u6bcf\u4e00\u884c\u90fd\u4f1a\u8fd0\u884c\u8be5\u8bed\u53e5\u5757\u3002\u8fd9\u4e09\u4e2a\u90e8\u5206\u7f3a\u5c11\u4efb\u4f55\u4e00\u90e8\u5206\u90fd\u53ef\u4ee5\u3002<\/p>\n<h4>5.4.1\u6848\u4f8b<\/h4>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;BEGIN{x&#061;0};\/\/bin\/bash$\/ {x&#043;&#043;;print x,$0};END {print x}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u7edf\u8ba1\u4ee5\/ bin\/bash\u7ed3\u5c3e\u7684\u884c\u6570&#xff0c;\u7b49\u540c\u4e8e<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#grep -c &#034;\/bin\/bash$&#034; \/etc\/passwd<\/span><\/p>\n<p>BEGIN\u6a21\u5f0f\u8868\u793a&#xff0c;\u5728\u5904\u7406\u6307\u5b9a\u7684\u6587\u672c\u4e4b\u524d&#xff0c;\u9700\u8981\u5148\u6267\u884cBEGIN\u6a21\u5f0f\u4e2d\u6307\u5b9a\u7684\u52a8\u4f5c; awk\u518d\u5904\u7406\u6307\u5b9a\u7684\u6587\u672c&#xff0c;\u4e4b\u540e\u518d\u6267\u884cEND\u6a21\u5f0f\u4e2d\u6307\u5b9a\u7684\u52a8\u4f5c&#xff0c;END{}\u8bed\u53e5\u5757\u4e2d&#xff0c;\u5f80\u5f80\u4f1a\u653e\u5165\u6253\u5370\u7ed3\u679c\u7b49\u8bed\u53e5<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F &#034;:&#034; &#039;! ($3&lt;200){print} &#039; \/etc\/passwd<\/span><\/p>\n<p>\u8f93\u51fa\u7b2c3\u4e2a\u5b57\u6bb5\u7684\u503c&#xff0c;\u4e0d\u5c0f\u4e8e200\u7684\u884c<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18lbjgmfzr4iq.png\" alt=\"image-20240811024833463\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk &#039;BEGIN {FS&#061;&#034;:&#034;} ;{if($3&gt;&#061;1000){print}}&#039; \/etc\/passwd<\/span><\/p>\n<p>\u5148\u5904\u7406\u5b8cBEGIN\u7684\u5185\u5bb9&#xff0c;\u518d\u6253\u5370\u6587\u672c\u91cc\u9762\u7684\u5185\u5bb9<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F &#034;:&#034; &#039; {max&#061;($3&gt;&#061;$4) ?$3:$4; {print max}} &#039; \/etc\/passwd<\/span><\/p>\n<p>(\u4e86\u89e3)($3&gt;$4)?$3:$4\u4e09\u5143\u8fd0\u7b97\u7b26&#xff0c;\u5982\u679c\u7b2c3\u4e2a\u5b57\u6bb5\u7684\u503c\u5927\u4e8e\u7b49\u4e8e\u7b2c4\u4e2a\u5b57\u6bb5\u7684\u503c&#xff0c;\u5219\u628a\u7b2c3\u4e2a\u5b57\u6bb5\u7684\u503c\u8d4b\u7ed9max&#xff0c;\u5426\u5219\u7b2c4\u4e2a\u5b57\u6bb5\u7684\u503c\u8d4b\u7ed9max<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18cljjt4io5l5.png\" alt=\"image-20240811024902717\" \/><\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F &#034;:&#034; &#039;{print NR,$0}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18mdyxg0tcp5c.png\" alt=\"image-20240811024921936\" \/><\/p>\n<p>\u8f93\u51fa\u6bcf\u884c\u5185\u5bb9\u548c\u884c\u53f7&#xff0c;\u6bcf\u5904\u7406\u5b8c\u4e00\u6761\u8bb0\u5f55&#xff0c;NR\u503c\u52a01<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#sed -n &#039;&#061;;p&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F &#034;:&#034; &#039;$7&#034;bash&#034;{print $1,47}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-1831kemc342d4.png\" alt=\"image-20240811024946347\" \/><\/p>\n<p><img decoding=\"async\" src=\"2025-04-182yfmygxvfe5.png\" alt=\"image-20240811025002881\" \/><\/p>\n<p>\u8f93\u51fa\u4ee5\u5192\u53f7\u5206\u9694\u4e14\u7b2c7\u4e2a\u5b57\u6bb5\u4e2d\u5305\u542b\/bash\u7684\u884c\u7684\u7b2c1\u4e2a\u5b57\u6bb5<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F: &#039;\/bash\/ {print $1}&#039; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost <span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F&#034;:&#034;&#039;($1&#034;root&#034;) &amp;&amp; (NF&#061;&#061;7) {print $1,$2,$NF } &#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18hpfpjc5au0p.png\" alt=\"image-20240811025026629\" \/><\/p>\n<p>\/etc\/passwd\u7b2c1\u4e2a\u5b57\u6bb5\u4e2d\u5305\u542broot\u4e14\u67097\u4e2a\u5b57\u6bb5\u7684\u884c\u7684\u7b2c1\u30012\u4e2a\u5b57\u6bb5<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F &#034;:&#034; &#039;($7!&#061;&#034;\/bin\/bash&#034;)&amp;&amp;($7!&#061;&#034;\/sbin\/nologin&#034;){print} &#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18mo3pnrchnwa.png\" alt=\"image-20240811025049624\" \/><\/p>\n<p>\u8f93\u51fa\u7b2c7\u4e2a\u5b57\u6bb5\u65e2\u4e0d\u4e3a\/bin\/bash&#xff0c;\u4e5f\u4e0d\u4e3a\/sbin\/nologin\u7684\u6240\u6709\u884c<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F: &#039;($NF !&#061;&#034; \/bin\/bash&#034;)&amp;&amp;($NF !&#061;&#034; \/sbin\/nologin&#034; ){print NR, $0}&#039; passwd<\/span><\/p>\n<p>\u901a\u8fc7\u7ba1\u9053\u3001\u53cc\u5f15\u53f7\u8c03\u7528shell \u547d\u4ee4:<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#echo $PATH | awk &#039;BEGIN{RS&#061;&#034;:&#034;};END {print NR}&#039;<\/span><\/p>\n<p>\u7edf\u8ba1\u4ee5\u5192\u53f7\u5206\u9694\u7684\u6587\u672c\u6bb5\u843d\u6570&#xff0c;END{ }\u8bed\u53e5\u5757\u4e2d&#xff0c;\u5f80\u5f80\u4f1a\u653e\u5165\u6253\u5370\u7ed3\u679c\u7b49\u8bed\u53e5<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#echo $PATH | awk &#039;BEGIN{RS&#061;&#034;:&#034;};{print NR,$0};END {print NR}&#039;<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F: &#039;\/bash$\/{print | &#034;wc -l&#034;}&#039; \/etc\/passwd<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18aa3rmv320kn.png\" alt=\"image-20240811025124007\" \/><\/p>\n<p>\u8c03\u7528wc -l\u547d\u4ee4\u7edf\u8ba1\u4f7f\u7528bash \u7684\u7528\u6237\u4e2a\u6570,\u7b49\u540c\u4e8e<\/p>\n<p><span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#grep -c &#034;bash$&#034; \/etc\/passwd<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#awk -F: &#039;\/bash$\/ {print}&#039; \/etc\/passwd | wc -l<\/span><br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost ~<span class=\"token punctuation\">]<\/span><span class=\"token comment\">#free -m |awk &#039;\/Mem:\/ {print int($3\/($3&#043;$4)100)&#034;%&#034;}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18x4egkxpr0v4.png\" alt=\"image-20240811025209921\" \/><\/p>\n<p>\u67e5\u770b\u5f53\u524d\u5185\u5b58\u4f7f\u7528\u767e\u5206\u6bd4<\/p>\n<p>[ root&#064;localhost ~]# free -m | awk &#039;\/Mem:\/ {print $3\/$2}&#039;<br \/>\n0.327869<br \/>\n[root&#064;localhost ~]# free -m | awk &#039;\/Mem:\/ {print $3\/$2100}&#039;<br \/>\n32.7869<br \/>\n[ root&#064;localhost ~]# free -m | awk &#039;\/Mem:\/ {print int($3\/$2100)}&#039;<br \/>\n32<br \/>\n[root&#064;localhost ~]# free -m | awk &#039;\/Mem:\/ {print int($3\/$2100)&#034;%&#034;}&#039;<br \/>\n32%<br \/>\n[root&#064;localhost ~]#free -m | awk &#039;\/Mem:\/ {print $3\/$2*100}&#039; | awk -F. &#039;{print $1&#034;%&#034;}&#039;<br \/>\ntop -b -n 1 | grep Cpu | awk -F &#039;,&#039; &#039;{print $4}&#039;| awk &#039;{print $1}&#039;<\/p>\n<p><img decoding=\"async\" src=\"2025-04-182g5n1hwqvp4.png\" alt=\"image-20240811025301515\" \/><\/p>\n<p>\u67e5\u770b\u5f53\u524dCPU\u7a7a\u95f2\u7387&#xff0c;(-b -n 1\u8868\u793a\u53ea\u9700\u89811\u6b21\u7684\u8f93\u51fa\u7ed3\u679c)<\/p>\n<p>dao 100%<br \/>\ndate -d &#034;$(awk -F &#034;.&#034; &#039;{print $1}&#039;\/proc\/uptime) second ago&#034;&#043;&#034;%F%H:%M:%S&#034;<\/p>\n<p>\u4e86\u89e3\u663e\u793a\u4e0a\u6b21\u7cfb\u7edf\u91cd\u542f\u65f6\u95f4&#xff0c;\u7b49\u540c\u4e8euptime; second ago\u4e3a\u663e\u793a\u591a\u5c11\u79d2\u524d\u7684\u65f6\u95f4&#xff0c;&#043;$\u201d \u9009\u65e5:3N:3S\u7b49\u540c\u4e8e&#043;&#034;3Y-n-%d 3\u65e5:8N:8S&#034;\u7684\u65f6\u95f4\u683c\u5f0f<\/p>\n<p>date -d &#034;$(awk -F &#034;.&#034; &#039;{iprint $1}&#039;\/proc\/uptime) second ago&#034;&#043;&#034;\u53f7F%H:%M:%S&#034;<\/p>\n<p>\u4e86\u89e3\u663e\u793a\u4e0a\u6b21\u7cfb\u7edf\u91cd\u542f\u65f6\u95f4&#xff0c;\u7b49\u540c\u4e8euptime; second ago\u4e3a\u663e\u793a\u591a\u5c11\u79d2\u524d\u7684\u65f6\u95f4&#xff0c;&#043;\u201cF\u6089\u65e5:M::S&#034;\u7b49\u540c\u4e8e&#043;\u201d%1-tm-d \u65e5: 38:8S&#034;\u7684\u65f6\u95f4\u683c\u5f0f<\/p>\n<p><span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN {n&#061;0 ; while (&#034;w&#034; | getline) n&#043;&#043; ; {print n-2}}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18gkd5sgq2rl4.png\" alt=\"image-20240811025347090\" \/><\/p>\n<p>\u8c03\u7528w\u547d\u4ee4&#xff0c;\u5e76\u7528\u6765\u7edf\u8ba1\u5728\u7ebf\u7528\u6237\u6570<\/p>\n<h4>5.4.2\u6ce8&#xff1a;\u5148\u505a\u5982\u4e0b\u5b9e\u9a8c<\/h4>\n<p><span class=\"token function\">seq<\/span> <span class=\"token number\">10<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039; {getline; print $0 }&#039;<\/span><br \/>\n<span class=\"token function\">seq<\/span> <span class=\"token number\">10<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039; { print $0 ; getline } &#039;<\/span><\/p>\n<p><span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN { &#034;hostname&#034; | getline ; {print $0}}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18sqhetkhie0b.png\" alt=\"image-20240811025418913\" \/><\/p>\n<p>\u8c03\u7528hostname,\u5e76\u8f93\u51fa\u5f53\u524d\u7684\u4e3b\u673a\u540d<\/p>\n<p>\u5f53getline\u5de6\u53f3\u65e0\u91cd\u5b9a\u5411\u7b26&#034;&lt;\u201c\u6216&#034;I&#034;\u65f6&#xff0c;awk\u9996\u5148\u8bfb\u53d6\u5230\u4e86\u7b2c\u4e00\u884c&#xff0c;\u5c31\u662f1&#xff0c;\u7136\u540egetline&#xff0c;\u5c31\u5f97\u5230\u4e861\u4e0b\u9762\u7684\u7b2c\u4e8c\u884c&#xff0c;\u5c31\u662f2&#xff0c;\u56e0\u4e3agetline\u4e4b\u540e&#xff0c;awk\u4f1a\u6539\u53d8\u5bf9\u5e94\u7684NE&#xff0c;NR&#xff0c;FNR\u548c<span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"> <\/p>\n<p>         O <\/p>\n<p>         \u7b49\u5185\u90e8\u53d8\u91cf&#xff0c;\u6240\u4ee5\u6b64\u65f6\u7684 <\/p>\n<p>        O\u7b49\u5185\u90e8\u53d8\u91cf&#xff0c;\u6240\u4ee5\u6b64\u65f6\u7684 <\/p>\n<p>    <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 0.6833em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.0278em\">O<\/span><span class=\"mord cjk_fallback\">\u7b49\u5185\u90e8\u53d8\u91cf&#xff0c;\u6240\u4ee5\u6b64\u65f6\u7684<\/span><\/span><\/span><\/span><\/span>O\u7684\u503c\u5c31\u4e0d\u518d\u662f1&#xff0c;\u800c\u662f2\u4e86&#xff0c;\u7136\u540e\u5c06\u5b83\u6253\u5370\u51fa\u6765\u3002\u5f53getline\u5de6\u53f3\u6709\u91cd\u5b9a\u5411\u7b26\u201d&lt;&#034;\u6216&#034;I&#034;\u65f6&#xff0c;getline\u5219\u4f5c\u7528\u4e8e\u5b9a\u5411\u8f93\u5165\u6587\u4ef6&#xff0c;\u7531\u4e8e\u8be5\u6587\u4ef6\u662f\u521a\u6253\u5f00&#xff0c;\u5e76\u6ca1\u6709\u88abawk\u8bfb\u5165\u4e00\u884c,\u53ea\u662fgetline\u8bfb\u5165&#xff0c;\u90a3\u4e48getline\u8fd4\u56de\u7684\u662f\u8be5\u6587\u4ef6\u7684\u7b2c\u4e00\u884c&#xff0c;\u800c\u4e0d\u662f\u9694\u884c\u3002<\/p>\n<h3>5.5CPU\u4f7f\u7528\u7387&#xff08;\u7b97\u4e0d&#xff09;<\/h3>\n<p><span class=\"token assign-left variable\">cpu_us<\/span><span class=\"token operator\">&#061;<\/span>top <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> Cpu <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $2}&#039;<\/span><br \/>\n<span class=\"token assign-left variable\">cpu_sy<\/span><span class=\"token operator\">&#061;<\/span>top <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> Cpu <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token parameter variable\">-F<\/span> <span class=\"token string\">&#039;,&#039;<\/span> <span class=\"token string\">&#039;{print $2}&#039;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $1}&#039;<\/span><br \/>\n<span class=\"token assign-left variable\">cpu_sum<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\"><span class=\"token variable\">$((<\/span>$cpu_us<span class=\"token operator\">&#043;<\/span>$cpu_sy<span class=\"token variable\">))<\/span><\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token variable\">$cpu_sum<\/span><br \/>\n<span class=\"token function\">vmstat<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{OFS&#061;&#034;|&#034;; print $0;$1&#061;$1;print $0}&#039;<\/span><\/p>\n<p>A B C D<br \/>\nA|B|C|D<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18vaiwk3fdaxj.png\" alt=\"image-20240811025455334\" \/><\/p>\n<p>$1&#061;$1\u662f\u7528\u6765\u6fc0\u6d3b$0\u7684\u91cd\u65b0\u8d4b\u503c&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u5b57\u6bb5$1\u2026\u548c\u5b57\u6bb5\u6570NF\u7684\u6539\u53d8\u4f1a\u4fc3\u4f7fawk\u91cd\u65b0\u8ba1\u7b97$0\u7684\u503c,\u901a\u5e38\u662f\u5728\u6539\u53d8OFS\u540e\u800c\u9700\u8981\u8f93\u51fa$0\u65f6\u8fd9\u6837\u505a<\/p>\n<p><span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{print $0;$1&#061;$1;print $0}&#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{print $0;$1&#061;$1;print $1,$2}&#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{$2&#061;$2;print $1,$2}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18dyr2ji5z4gd.png\" alt=\"image-20240811025526220\" \/><\/p>\n<h2>\u516d\u3001\u6700\u540e\u603b\u7ed3<\/h2>\n<p>grep \u548c egrep \u6587\u672c\u8fc7\u6ee4 (\u66f4\u9002\u5408\u5355\u7eaf\u7684\u67e5\u627e\u6216\u5339\u914d\u6587\u672c) sed \u6d41\u7f16\u8f91\u5668 \u5b9e\u73b0\u7f16\u8f91\u7684(\u66f4\u9002\u5408\u7f16\u8f91\u5339\u914d\u5230\u7684\u6587\u672c) e\u5de6\u53f3\u6709\u91cd\u5b9a\u5411\u7b26&#034;&lt;&#034;\u6216&#034;I&#034;\u65f6&#xff0c;getline\u5219\u4f5c\u7528\u4e8e\u5b9a\u5411\u8f93\u5165\u6587\u4ef6&#xff0c;\u7531\u4e8e\u8be5\u6587\u4ef6\u662f\u521a\u6253\u5f00&#xff0c;\u5e76\u6ca1\u6709\u88abawk\u8bfb\u5165\u4e00\u884c,\u53ea\u662fgetline\u8bfb\u5165&#xff0c;\u90a3\u4e48getline\u8fd4\u56de\u7684\u662f\u8be5\u6587\u4ef6\u7684\u7b2c\u4e00\u884c&#xff0c;\u800c\u4e0d\u662f\u9694\u884c\u3002<\/p>\n<h3>5.5CPU\u4f7f\u7528\u7387&#xff08;\u7b97\u4e0d&#xff09;<\/h3>\n<p><span class=\"token assign-left variable\">cpu_us<\/span><span class=\"token operator\">&#061;<\/span>top <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> Cpu <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $2}&#039;<\/span><br \/>\n<span class=\"token assign-left variable\">cpu_sy<\/span><span class=\"token operator\">&#061;<\/span>top <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> Cpu <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token parameter variable\">-F<\/span> <span class=\"token string\">&#039;,&#039;<\/span> <span class=\"token string\">&#039;{print $2}&#039;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $1}&#039;<\/span><br \/>\n<span class=\"token assign-left variable\">cpu_sum<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\"><span class=\"token variable\">$((<\/span>$cpu_us<span class=\"token operator\">&#043;<\/span>$cpu_sy<span class=\"token variable\">))<\/span><\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token variable\">$cpu_sum<\/span><br \/>\n<span class=\"token function\">vmstat<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{OFS&#061;&#034;|&#034;; print $0;$1&#061;$1;print $0}&#039;<\/span><\/p>\n<p>A B C D<br \/>\nA|B|C|D<\/p>\n<p><img decoding=\"async\" src=\"2025-04-18gfibelwm2bv.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>$1&#061;$1\u662f\u7528\u6765\u6fc0\u6d3b$0\u7684\u91cd\u65b0\u8d4b\u503c&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u5b57\u6bb5$1\u2026\u548c\u5b57\u6bb5\u6570NF\u7684\u6539\u53d8\u4f1a\u4fc3\u4f7fawk\u91cd\u65b0\u8ba1\u7b97$0\u7684\u503c,\u901a\u5e38\u662f\u5728\u6539\u53d8OFS\u540e\u800c\u9700\u8981\u8f93\u51fa$0\u65f6\u8fd9\u6837\u505a<\/p>\n<p><span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{print $0;$1&#061;$1;print $0}&#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{print $0;$1&#061;$1;print $1,$2}&#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;A B C D&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;BEGIN{OFS&#061;&#034;|&#034;};{$2&#061;$2;print $1,$2}&#039;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2025-04-18invb3o50ncr.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\u516d\u3001\u6700\u540e\u603b\u7ed3<\/h2>\n<p>grep \u548c egrep \u6587\u672c\u8fc7\u6ee4 (\u66f4\u9002\u5408\u5355\u7eaf\u7684\u67e5\u627e\u6216\u5339\u914d\u6587\u672c) sed \u6d41\u7f16\u8f91\u5668 \u5b9e\u73b0\u7f16\u8f91\u7684(\u66f4\u9002\u5408\u7f16\u8f91\u5339\u914d\u5230\u7684\u6587\u672c) awk \u6587\u672c\u62a5\u544a\u751f\u6210\u5668 \u5b9e\u73b0\u683c\u5f0f\u5316\u6587\u672c\u8f93\u51fa(\u66f4\u9002\u5408\u683c\u5f0f\u5316\u6587\u672c&#xff0c;\u5bf9\u6587\u672c\u8fdb\u884c\u8f83\u590d\u6742\u683c\u5f0f\u5904\u7406)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4w\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf49\u6b21\u3002\u200b\t\tAWK \u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002\u5b83\u662f\u4e13\u95e8\u4e3a\u6587\u672c\u5904\u7406\u8bbe\u8ba1\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4e5f\u662f\u884c\u5904\u7406\u8f6f\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u626b\u63cf\u3001\u8fc7\u6ee4\u3001\u7edf\u8ba1\u6c47\u603b\u5de5\u4f5c\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6807\u51c6\u8f93\u5165\u4e5f\u53ef\u4ee5\u662f\u7ba1\u9053\u6216\u6587\u4ef6\u3002\u200b\t\t20\u4e16\u7eaa70\u5e74\u4ee3\u8bde\u751f\u4e8e\u8d1d\u5c14\u5b9e\u9a8c\u5ba4\uff0c\u73b0\u5728centos7\u7528\u7684\u662fgawk\u4e4b\u6240\u4ee5\u53eb AWK \u662f\u56e0\u4e3a\u5176\u53d6\u4e86\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho\uff0cPeter Weinberger, \u548c Brian Kernighan \u7684 Family Name \u7684\u9996\u5b57\u7b26\u3002_awk\u547d\u4ee4\u8be6\u89e3<\/p>\n","protected":false},"author":2,"featured_media":9633,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[352,130,58,190,43],"topic":[],"class_list":["post-9653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-bash","tag-chrome","tag-linux","tag-190","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/9653.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4w\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf49\u6b21\u3002\u200b AWK \u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002\u5b83\u662f\u4e13\u95e8\u4e3a\u6587\u672c\u5904\u7406\u8bbe\u8ba1\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4e5f\u662f\u884c\u5904\u7406\u8f6f\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u626b\u63cf\u3001\u8fc7\u6ee4\u3001\u7edf\u8ba1\u6c47\u603b\u5de5\u4f5c\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6807\u51c6\u8f93\u5165\u4e5f\u53ef\u4ee5\u662f\u7ba1\u9053\u6216\u6587\u4ef6\u3002\u200b 20\u4e16\u7eaa70\u5e74\u4ee3\u8bde\u751f\u4e8e\u8d1d\u5c14\u5b9e\u9a8c\u5ba4\uff0c\u73b0\u5728centos7\u7528\u7684\u662fgawk\u4e4b\u6240\u4ee5\u53eb AWK \u662f\u56e0\u4e3a\u5176\u53d6\u4e86\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho\uff0cPeter Weinberger, \u548c Brian Kernighan \u7684 Family Name \u7684\u9996\u5b57\u7b26\u3002_awk\u547d\u4ee4\u8be6\u89e3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/9653.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T09:29:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b906d50d.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9653.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/9653.html\",\"name\":\"awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T09:29:55+00:00\",\"dateModified\":\"2025-04-18T09:29:55+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/9653.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/9653.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9653.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"awk\u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/9653.html","og_locale":"zh_CN","og_type":"article","og_title":"awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4w\u6b21\uff0c\u70b9\u8d5e31\u6b21\uff0c\u6536\u85cf49\u6b21\u3002\u200b AWK \u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002\u5b83\u662f\u4e13\u95e8\u4e3a\u6587\u672c\u5904\u7406\u8bbe\u8ba1\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4e5f\u662f\u884c\u5904\u7406\u8f6f\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u626b\u63cf\u3001\u8fc7\u6ee4\u3001\u7edf\u8ba1\u6c47\u603b\u5de5\u4f5c\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6807\u51c6\u8f93\u5165\u4e5f\u53ef\u4ee5\u662f\u7ba1\u9053\u6216\u6587\u4ef6\u3002\u200b 20\u4e16\u7eaa70\u5e74\u4ee3\u8bde\u751f\u4e8e\u8d1d\u5c14\u5b9e\u9a8c\u5ba4\uff0c\u73b0\u5728centos7\u7528\u7684\u662fgawk\u4e4b\u6240\u4ee5\u53eb AWK \u662f\u56e0\u4e3a\u5176\u53d6\u4e86\u4e09\u4f4d\u521b\u59cb\u4eba Alfred Aho\uff0cPeter Weinberger, \u548c Brian Kernighan \u7684 Family Name \u7684\u9996\u5b57\u7b26\u3002_awk\u547d\u4ee4\u8be6\u89e3","og_url":"https:\/\/www.wsisp.com\/helps\/9653.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T09:29:55+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418092952-68021b906d50d.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/9653.html","url":"https:\/\/www.wsisp.com\/helps\/9653.html","name":"awk\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T09:29:55+00:00","dateModified":"2025-04-18T09:29:55+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/9653.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/9653.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/9653.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"awk\u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/9653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=9653"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/9653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/9633"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=9653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=9653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=9653"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=9653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}