{"id":89997,"date":"2026-08-04T13:09:16","date_gmt":"2026-08-04T05:09:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/89997.html"},"modified":"2026-08-04T13:09:16","modified_gmt":"2026-08-04T05:09:16","slug":"shell-%e4%b8%89%e5%89%91%e5%ae%a2%ef%bc%9agrep%e3%80%81sed%e3%80%81awk-%e6%89%be%e3%80%81%e6%94%b9%e3%80%81%e7%ae%97%ef%bc%8c%e4%b8%80%e4%b8%aa%e9%83%bd%e4%b8%8d%e8%83%bd%e5%b0%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/89997.html","title":{"rendered":"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11"},"content":{"rendered":"<h2>Shell \u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97&#xff0c;\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11<\/h2>\n<h3>\u5f00\u5934<\/h3>\n<p>\u5b66 Linux \u5230\u8fd9\u4f1a\u513f&#xff0c;\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u670d\u52a1\u63a7\u5236\u8fd9\u4e9b\u6211\u591a\u5c11\u90fd\u80fd\u4e0a\u624b\u4e86\u3002\u4f46\u6bcf\u6b21\u770b\u7f51\u4e0a\u6559\u7a0b\u91cc\u90a3\u79cd&#034;\u4e00\u6761\u547d\u4ee4\u5904\u7406\u65e5\u5fd7&#034;\u7684\u5199\u6cd5\u2014\u2014cat xxx.log | grep \u62a5\u9519 | awk &#039;{print $2}&#039; | sort | uniq -c\u2014\u2014\u6211\u5c31\u5934\u76ae\u53d1\u9ebb&#xff0c;\u611f\u89c9\u50cf\u5728\u770b\u5916\u661f\u6587\u3002<\/p>\n<p>\u8fd9\u5468\u8001\u5e08\u6b63\u5f0f\u8bb2\u4e86\u4f20\u8bf4\u4e2d\u7684\u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001awk\u3002\u540d\u5b57\u542c\u7740\u633a\u552c\u4eba&#xff0c;\u5176\u5b9e\u4e09\u4e2a\u90fd\u662f\u5904\u7406\u6587\u672c\u7684\u5de5\u5177\u3002\u4f46\u4e00\u5f00\u59cb\u6211\u5b8c\u5168\u5206\u4e0d\u6e05\u5b83\u4eec\u8c01\u662f\u8c01\u2014\u2014\u4e0d\u90fd\u662f&#034;\u5bf9\u6587\u672c\u505a\u70b9\u4ec0\u4e48&#034;\u5417&#xff1f;\u4e3a\u5565\u8981\u641e\u4e09\u4e2a&#xff1f;<\/p>\n<p>\u540e\u6765\u8001\u5e08\u4e00\u53e5\u8bdd\u70b9\u9192\u4e86\u6211&#xff1a;<\/p>\n<p>grep \u8d1f\u8d23&#034;\u627e&#034;&#xff0c;sed \u8d1f\u8d23&#034;\u6539&#034;&#xff0c;awk \u8d1f\u8d23&#034;\u53d6\u5217\u548c\u7b97&#034;\u3002<\/p>\n<p>\u6587\u672c\u5904\u7406\u65e0\u975e\u5c31\u8fd9\u4e09\u4ef6\u4e8b\u3002\u8fd9\u7bc7\u5c31\u628a\u6211\u5b66\u4e09\u5251\u5ba2\u7684\u8fc7\u7a0b\u3001\u8e29\u7684\u5751\u3001\u8fd8\u6709\u8fde\u62db\u73a9\u6cd5\u90fd\u8bb0\u4e0b\u6765\u3002<\/p>\n<p>\u73af\u5883\u8fd8\u662f\u90a3\u53f0 CentOS 7 \u865a\u62df\u673a&#xff0c;VMware \u91cc\u8dd1\u7684\u3002\u524d\u9762\u88ab\u914d\u7f6e\u6587\u4ef6\u641e\u5d29\u8fc7\u4e00\u6b21&#xff0c;\u8fd9\u6b21\u5b66\u4e09\u5251\u5ba2\u4e4b\u524d\u5148\u6253\u4e86\u4e2a\u5feb\u7167\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u5251\u5ba2\u5230\u5e95\u5404\u7ba1\u54ea\u644a\u4e8b&#xff1f;<\/h3>\n<p>\u5148\u5efa\u4e2a\u6574\u4f53\u8ba4\u77e5&#xff0c;\u540e\u9762\u5c31\u4e0d\u5bb9\u6613\u6df7&#xff1a;<\/p>\n<table>\n<tr>\u5de5\u5177\u540d\u5b57\u6765\u6e90\u4e00\u53e5\u8bdd\u5b9a\u4f4d\u9ed8\u8ba4\u5e72\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>grep<\/td>\n<td>\u6e90\u81ea ed \u7f16\u8f91\u5668\u7684 g\/re\/p&#xff08;\u5168\u5c40\u641c\u7d22\u6b63\u5219\u5e76\u6253\u5370&#xff09;<\/td>\n<td>\u627e\u884c<\/td>\n<td>\u628a&#034;\u7b26\u5408\u6761\u4ef6&#034;\u7684\u884c\u8fc7\u6ee4\u51fa\u6765<\/td>\n<\/tr>\n<tr>\n<td>sed<\/td>\n<td>Stream Editor&#xff0c;\u6d41\u5f0f\u7f16\u8f91\u5668<\/td>\n<td>\u6539<\/td>\n<td>\u5bf9\u6587\u672c\u505a\u66ff\u6362\u3001\u5220\u9664\u3001\u63d2\u5165&#xff0c;\u975e\u4ea4\u4e92<\/td>\n<\/tr>\n<tr>\n<td>awk<\/td>\n<td>\u4e09\u4e2a\u4f5c\u8005 Aho\u3001Weinberger\u3001Kernighan \u7684\u9996\u5b57\u6bcd<\/td>\n<td>\u53d6\u5217\/\u7b97<\/td>\n<td>\u6309\u5217\u62c6\u5206&#xff0c;\u53d6\u5b57\u6bb5\u3001\u505a\u7edf\u8ba1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u62ff\u540c\u4e00\u6bb5\u6570\u636e\u8bd5\u8bd5\u6c34&#xff0c;\u4e09\u5144\u5f1f\u5404\u51fa\u624b&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5047\u8bbe \/etc\/passwd \u91cc\u6709\u4e00\u884c&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># root:x:0:0:root:\/root:\/bin\/bash<\/span><\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;root&#034;<\/span> \/etc\/passwd              <span class=\"token comment\"># \u628a\u5305\u542b root \u7684\u6574\u884c\u635e\u51fa\u6765<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/bash\/zsh\/&#039;<\/span> \/etc\/passwd        <span class=\"token comment\"># \u628a\u6bcf\u884c\u91cc\u7684 bash \u66ff\u6362\u6210 zsh<\/span><br \/>\n<span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;{print $1}&#039;<\/span> \/etc\/passwd     <span class=\"token comment\"># \u6309\u5192\u53f7\u62c6\u5f00&#xff0c;\u53ea\u53d6\u7b2c\u4e00\u4e2a\u5b57\u6bb5&#xff08;\u7528\u6237\u540d&#xff09;<\/span><\/p>\n<p>\u6211\u5f53\u65f6\u7684\u7406\u89e3\u662f&#xff1a;grep \u7ba1&#034;\u884c&#034;&#xff0c;awk \u7ba1&#034;\u5217&#034;&#xff0c;sed \u7ba1&#034;\u884c\u91cc\u9762\u7684\u5185\u5bb9&#034;\u3002\u867d\u7136\u4e0d\u4e25\u8c28&#xff0c;\u4f46\u505a\u9898\u65f6\u591f\u7528\u3002<\/p>\n<p>\u771f\u6b63\u5389\u5bb3\u7684\u7528\u6cd5\u662f\u628a\u5b83\u4eec\u4e32\u8d77\u6765&#xff0c;\u5404\u5e72\u4e00\u6bb5&#xff1a;<\/p>\n<p>\u65e5\u5fd7\u6587\u4ef6 \u2192 grep \u7b5b\u51fa\u8981\u7684\u884c \u2192 sed \u628a\u683c\u5f0f\u6539\u4e00\u6539 \u2192 awk \u53d6\u51fa\u5b57\u6bb5 \u2192 sort \/ uniq \u7edf\u8ba1<\/p>\n<hr \/>\n<h3>1. grep\u2014\u2014\u628a\u6211\u8981\u7684\u884c\u635e\u51fa\u6765<\/h3>\n<h4>\u57fa\u672c\u7528\u6cd5<\/h4>\n<p>grep \u6700\u6734\u7d20\u7684\u7528\u6cd5&#xff0c;\u5c31\u662f\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\u641c\u5173\u952e\u8bcd&#xff1a;<\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;root&#034;<\/span> \/etc\/passwd<br \/>\n<span class=\"token comment\"># root:x:0:0:root:\/root:\/bin\/bash<\/span><br \/>\n<span class=\"token comment\"># operator:x:11:0:operator:\/root:\/sbin\/nologin<\/span><br \/>\n<span class=\"token comment\"># &#8230;&#8230;<\/span><\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;ERROR&#034;<\/span> \/var\/log\/messages     <span class=\"token comment\"># \u5728\u65e5\u5fd7\u91cc\u627e\u62a5\u9519<\/span><\/p>\n<p>\u6ca1\u7ed9\u6587\u4ef6\u7684\u65f6\u5019&#xff0c;grep \u4f1a\u4ece\u6807\u51c6\u8f93\u5165\u8bfb&#xff0c;\u6240\u4ee5\u914d\u5408\u7ba1\u9053\u662f\u5b83\u7684\u4e3b\u6218\u573a&#xff1a;<\/p>\n<p><span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> sshd          <span class=\"token comment\"># \u8fdb\u7a0b\u5217\u8868\u91cc\u627e sshd<\/span><br \/>\n<span class=\"token function\">history<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;vim&#034;<\/span>        <span class=\"token comment\"># \u5386\u53f2\u547d\u4ee4\u91cc\u7ffb vim \u76f8\u5173\u7684<\/span><\/p>\n<h4>\u5e38\u7528\u53c2\u6570<\/h4>\n<table>\n<tr>\u53c2\u6570\u4f5c\u7528\u4f8b\u5b50<\/tr>\n<tbody>\n<tr>\n<td>-i<\/td>\n<td>\u5ffd\u7565\u5927\u5c0f\u5199<\/td>\n<td>grep -i error log&#xff08;\u80fd\u5339\u914d\u5230 ERROR\/Error&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>-v<\/td>\n<td>\u53cd\u5411\u5339\u914d&#xff0c;\u8f93\u51fa&#034;\u4e0d\u542b&#034;\u5173\u952e\u8bcd\u7684\u884c<\/td>\n<td>grep -v &#034;^#&#034; sshd_config&#xff08;\u53bb\u6389\u6ce8\u91ca\u884c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>-n<\/td>\n<td>\u663e\u793a\u884c\u53f7<\/td>\n<td>grep -n &#034;server&#034; config<\/td>\n<\/tr>\n<tr>\n<td>-c<\/td>\n<td>\u53ea\u6570\u5339\u914d\u7684\u884c\u6570<\/td>\n<td>grep -c &#034;error&#034; log<\/td>\n<\/tr>\n<tr>\n<td>-r<\/td>\n<td>\u9012\u5f52\u641c\u6574\u4e2a\u76ee\u5f55<\/td>\n<td>grep -r &#034;port&#034; \/etc\/nginx\/<\/td>\n<\/tr>\n<tr>\n<td>-o<\/td>\n<td>\u53ea\u8f93\u51fa\u5339\u914d\u7684\u90e8\u5206&#xff0c;\u4e0d\u8f93\u51fa\u6574\u884c<\/td>\n<td>echo &#034;abc123def456&#034; | grep -oE &#034;[0-9]&#043;&#034;<\/td>\n<\/tr>\n<tr>\n<td>-w<\/td>\n<td>\u5168\u8bcd\u5339\u914d<\/td>\n<td>grep -w &#034;is&#034; file&#xff08;\u4e0d\u4f1a\u5339\u914d\u5230 this&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>-E<\/td>\n<td>\u7528\u6269\u5c55\u6b63\u5219<\/td>\n<td>grep -E &#034;(error|warning)&#034; log<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u6b63\u5219\u5165\u95e8\u2014\u2014grep \u662f\u6700\u597d\u7684\u7ec3\u624b\u5de5\u5177<\/h4>\n<p>\u5b66 grep \u987a\u4fbf\u628a\u6b63\u5219\u5165\u95e8\u4e86\u3002\u6700\u5e38\u7528\u7684\u51e0\u4e2a\u5143\u5b57\u7b26&#xff1a;<\/p>\n<table>\n<tr>\u5143\u5b57\u7b26\u542b\u4e49\u4f8b\u5b50<\/tr>\n<tbody>\n<tr>\n<td>.<\/td>\n<td>\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/td>\n<td>grep &#034;a.c&#034; \u80fd\u5339\u914d abc\u3001a1c<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>\u524d\u4e00\u4e2a\u5b57\u7b26\u91cd\u590d 0 \u6b21\u6216\u591a\u6b21<\/td>\n<td>grep &#034;ab*c&#034; \u80fd\u5339\u914d ac\u3001abc\u3001abbc<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>\u884c\u9996<\/td>\n<td>grep &#034;^root&#034; \/etc\/passwd<\/td>\n<\/tr>\n<tr>\n<td>$<\/td>\n<td>\u884c\u5c3e<\/td>\n<td>grep &#034;bash$&#034; \/etc\/passwd<\/td>\n<\/tr>\n<tr>\n<td>[abc]<\/td>\n<td>\u5b57\u7b26\u96c6\u5408<\/td>\n<td>grep &#034;gr[ae]y&#034; \u80fd\u5339\u914d gray \/ grey<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8e29\u5751&#xff1a;\u57fa\u672c\u6b63\u5219\u548c\u6269\u5c55\u6b63\u5219\u3002 \u6211\u7ec3 grep &#034;(error|warning)&#034; log \u7684\u65f6\u5019&#xff0c;\u5565\u4e5f\u641c\u4e0d\u5230\u3002\u540e\u6765\u624d\u77e5\u9053 grep \u9ed8\u8ba4\u7528\u7684\u662f\u57fa\u672c\u6b63\u5219&#xff0c;\u91cc\u9762 ( )\u3001&#043;\u3001?\u3001| \u8fd9\u4e9b&#034;\u9ad8\u7ea7&#034;\u7b26\u53f7\u90fd\u88ab\u5f53\u6210\u666e\u901a\u5b57\u7b26&#xff0c;\u5fc5\u987b\u52a0 \\\\ \u8f6c\u4e49&#xff0c;\u6216\u8005\u76f4\u63a5\u52a0 -E \u7528\u6269\u5c55\u6b63\u5219&#xff1a;<\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;\\\\(error\\\\|warning\\\\)&#034;<\/span> log    <span class=\"token comment\"># \u57fa\u672c\u6b63\u5219\u8981\u52a0\u53cd\u659c\u6760<\/span><br \/>\n<span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-E<\/span> <span class=\"token string\">&#034;(error|warning)&#034;<\/span> log    <span class=\"token comment\"># \u52a0 -E \u5c31\u4e0d\u7528\u8f6c\u4e49\u4e86&#xff08;\u63a8\u8350&#xff09;<\/span><br \/>\n<span class=\"token function\">egrep<\/span> <span class=\"token string\">&#034;(error|warning)&#034;<\/span> log      <span class=\"token comment\"># egrep \u5c31\u662f grep -E \u7684\u522b\u540d<\/span><\/p>\n<p>\u6211\u81f3\u4eca\u8bb0\u5f97 \\\\(&#8230;\\\\|&#8230;\\\\) \u8fd9\u79cd\u5199\u6cd5&#xff0c;\u592a\u53cd\u4eba\u7c7b\u4e86&#xff0c;\u73b0\u5728\u4e00\u5f8b grep -E\u3002<\/p>\n<h4>grep \u7684\u5751<\/h4>\n<p>\u57511&#xff1a;grep \u5339\u914d\u5230\u4e86\u5b83\u81ea\u5df1\u3002<\/p>\n<p><span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> sshd<br \/>\n<span class=\"token comment\"># \u8f93\u51fa\u91cc\u4f1a\u6df7\u8fdb\u4e00\u884c&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># root  12345  0.0  0.0  11292  4320 pts\/0  S&#043;  08:30  0:00 grep &#8211;color&#061;auto sshd<\/span><\/p>\n<p>\u56e0\u4e3a ps aux | grep sshd \u8fd9\u4e2a\u547d\u4ee4\u672c\u8eab\u4e5f\u5305\u542b \u201csshd\u201d \u51e0\u4e2a\u5b57&#xff0c;grep \u5c31\u628a\u81ea\u5df1\u90a3\u884c\u4e5f\u635e\u51fa\u6765\u4e86\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u518d\u52a0\u4e2a -v grep&#xff1a;<\/p>\n<p><span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> sshd <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-v<\/span> <span class=\"token function\">grep<\/span><\/p>\n<p>\u6211\u7b2c\u4e00\u6b21\u770b\u5230 grep \u81ea\u5df1\u51fa\u73b0\u5728\u7ed3\u679c\u91cc&#xff0c;\u8fd8\u4ee5\u4e3a\u7cfb\u7edf\u574f\u4e86&#xff0c;\u7422\u78e8\u4e86\u534a\u5929\u3002<\/p>\n<p>\u57512&#xff1a;\u5927\u5c0f\u5199\u3002 \u65e5\u5fd7\u91cc\u641c error&#xff0c;\u7ed3\u679c\u65e5\u5fd7\u91cc\u5199\u7684\u662f ERROR&#xff0c;\u5565\u4e5f\u6ca1\u641c\u5230\u3002\u641c\u4e4b\u524d\u5148\u5728\u8111\u5b50\u91cc\u8fc7\u4e00\u904d\u8981\u4e0d\u8981 -i\u3002<\/p>\n<p>\u57513&#xff1a;\u641c\u76ee\u5f55\u76f4\u63a5\u62a5\u9519\u3002 grep &#034;abc&#034; \/etc\/ \u4f1a\u62a5 grep: \/etc\/: Is a directory\u3002\u8981\u52a0 -r \u624d\u80fd\u9012\u5f52\u8fdb\u76ee\u5f55\u3002<\/p>\n<hr \/>\n<h3>2. sed\u2014\u2014\u4e0d\u6253\u5f00\u6587\u4ef6\u4e5f\u80fd\u6539\u6587\u4ef6<\/h3>\n<p>sed \u5168\u540d\u53eb Stream Editor&#xff08;\u6d41\u5f0f\u7f16\u8f91\u5668&#xff09;\u3002\u6211\u7b2c\u4e00\u6b21\u7406\u89e3\u5b83\u662f\u8fd9\u6837\u7684&#xff1a;vim \u662f&#034;\u6253\u5f00\u6587\u4ef6&#xff0c;\u6211\u624b\u52a8\u6539&#034;&#xff0c;sed \u662f&#034;\u4e0d\u6253\u5f00\u6587\u4ef6&#xff0c;\u4e00\u6761\u547d\u4ee4\u81ea\u52a8\u6539&#034;\u3002\u9002\u5408\u6279\u91cf\u5904\u7406&#xff0c;\u5c24\u5176\u662f\u6539\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<h4>\u66ff\u6362&#xff1a;s\/\u65e7\/\u65b0\/<\/h4>\n<p>\u6700\u5e38\u7528\u7684\u64cd\u4f5c\u662f\u66ff\u6362&#xff0c;\u8bed\u6cd5\u662f s\/\u65e7\u5185\u5bb9\/\u65b0\u5185\u5bb9\/&#xff1a;<\/p>\n<p><span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/root\/ROOT\/&#039;<\/span> \/etc\/passwd    <span class=\"token comment\"># \u628a\u6bcf\u884c\u7b2c\u4e00\u4e2a root \u6362\u6210 ROOT<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/root\/ROOT\/g&#039;<\/span> \/etc\/passwd   <span class=\"token comment\"># \u52a0 g&#xff0c;\u628a\u6bcf\u884c\u6240\u6709 root \u90fd\u6362\u6389<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/root\/ROOT\/2&#039;<\/span> \/etc\/passwd   <span class=\"token comment\"># \u6570\u5b57 2&#xff0c;\u53ea\u66ff\u6362\u6bcf\u884c\u7b2c 2 \u4e2a root<\/span><\/p>\n<p>\u5751&#xff1a;\u4e0d\u52a0 g \u53ea\u66ff\u6362\u6bcf\u884c\u7b2c\u4e00\u4e2a\u3002 \u6211\u6709\u6b21\u60f3\u628a\u914d\u7f6e\u91cc\u6240\u6709\u7684 localhost \u90fd\u6539\u6210\u670d\u52a1\u5668 IP&#xff0c;\u7528\u4e86 sed &#039;s\/localhost\/192.168.1.10\/&#039;&#xff0c;\u7ed3\u679c\u6bcf\u884c\u53ea\u6362\u4e86\u7b2c\u4e00\u4e2a&#xff0c;\u540e\u9762\u6ca1\u6362\u5230\u7684\u5168\u6f0f\u4e86\u3002\u540e\u6765\u624d\u53d1\u73b0\u8981\u52a0 g&#xff08;global&#xff0c;\u5168\u5c40&#xff09;\u624d\u4f1a\u66ff\u6362\u884c\u5185\u6240\u6709\u5339\u914d\u3002<\/p>\n<h4>\u6309\u884c\u53f7 \/ \u8303\u56f4\u64cd\u4f5c<\/h4>\n<p><span class=\"token function\">sed<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token string\">&#039;2p&#039;<\/span> <span class=\"token function\">file<\/span>        <span class=\"token comment\"># \u53ea\u6253\u5370\u7b2c 2 \u884c&#xff08;-n \u662f&#034;\u522b\u81ea\u52a8\u6253\u5370&#034;&#xff0c;p \u662f\u6253\u5370&#xff09;<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token string\">&#039;2,5p&#039;<\/span> <span class=\"token function\">file<\/span>      <span class=\"token comment\"># \u6253\u5370\u7b2c 2 \u5230 5 \u884c<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;3d&#039;<\/span> <span class=\"token function\">file<\/span>           <span class=\"token comment\"># \u5220\u9664\u7b2c 3 \u884c<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;5,10d&#039;<\/span> <span class=\"token function\">file<\/span>        <span class=\"token comment\"># \u5220\u9664\u7b2c 5 \u5230 10 \u884c<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;\/^#\/d&#039;<\/span> <span class=\"token function\">file<\/span>        <span class=\"token comment\"># \u5220\u9664\u6240\u6709\u4ee5 # \u5f00\u5934\u7684\u884c&#xff08;\u5220\u6ce8\u91ca&#xff09;<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/^#\/\/&#039;<\/span> <span class=\"token function\">file<\/span>       <span class=\"token comment\"># \u628a\u6bcf\u884c\u5f00\u5934\u7684 # \u53bb\u6389&#xff08;\u53d6\u6d88\u6ce8\u91ca&#xff09;<\/span><\/p>\n<p>-n \u548c p \u7ec4\u5408\u662f sed \u91cc\u7684\u7ecf\u5178\u642d\u914d&#xff0c;\u610f\u601d\u662f&#034;\u53ea\u6253\u5370\u6211\u60f3\u770b\u7684\u884c&#034;\u3002\u8fd9\u4e2a\u6211\u4e00\u5f00\u59cb\u8001\u662f\u5fd8&#xff0c;\u4e0d\u52a0 -n \u7684\u8bdd&#xff0c;\u6574\u4e2a\u6587\u4ef6\u4f1a\u88ab\u6253\u4e00\u904d&#xff0c;\u7136\u540e\u5339\u914d\u884c\u518d\u6253\u4e00\u904d&#xff0c;\u8f93\u51fa\u8001\u957f\u8001\u957f\u3002<\/p>\n<h4>\u63d2\u5165\u548c\u8ffd\u52a0<\/h4>\n<p><span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;2i hello&#039;<\/span> <span class=\"token function\">file<\/span>                   <span class=\"token comment\"># \u5728\u7b2c 2 \u884c\u4e0a\u9762\u63d2\u5165\u4e00\u884c hello&#xff08;i &#061; insert&#xff09;<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;2a world&#039;<\/span> <span class=\"token function\">file<\/span>                   <span class=\"token comment\"># \u5728\u7b2c 2 \u884c\u4e0b\u9762\u8ffd\u52a0\u4e00\u884c world&#xff08;a &#061; append&#xff09;<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;\/root\/a this is root&#039;<\/span> <span class=\"token function\">file<\/span>       <span class=\"token comment\"># \u5728\u5305\u542b root \u7684\u884c\u540e\u9762\u8ffd\u52a0\u4e00\u884c<\/span><\/p>\n<h4>\u539f\u5730\u4fee\u6539&#xff1a;-i&#xff0c;\u8840\u6cea\u6559\u8bad<\/h4>\n<p>\u524d\u9762\u6240\u6709\u64cd\u4f5c&#xff0c;\u4e0d\u5199 -i \u90fd\u53ea\u662f\u6253\u5370\u5230\u5c4f\u5e55\u4e0a&#xff0c;\u6587\u4ef6\u672c\u8eab\u6ca1\u53d8\u3002\u8981\u771f\u7684\u6539\u6587\u4ef6&#xff0c;\u5fc5\u987b\u52a0 -i&#xff1a;<\/p>\n<p><span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s\/old\/new\/g&#039;<\/span> <span class=\"token function\">file<\/span>          <span class=\"token comment\"># \u53ea\u663e\u793a\u7ed3\u679c&#xff0c;\u6587\u4ef6\u6ca1\u52a8<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token parameter variable\">-i<\/span> <span class=\"token string\">&#039;s\/old\/new\/g&#039;<\/span> <span class=\"token function\">file<\/span>       <span class=\"token comment\"># \u76f4\u63a5\u6539\u6587\u4ef6&#xff08;\u5371\u9669&#xff0c;\u6539\u5b8c\u4e0d\u53ef\u9006&#xff09;<\/span><br \/>\n<span class=\"token function\">sed<\/span> <span class=\"token parameter variable\">-i.bak<\/span> <span class=\"token string\">&#039;s\/old\/new\/g&#039;<\/span> <span class=\"token function\">file<\/span>   <span class=\"token comment\"># \u6539\u6587\u4ef6&#xff0c;\u540c\u65f6\u628a\u539f\u6587\u4ef6\u5907\u4efd\u6210 file.bak<\/span><\/p>\n<p>\u6211\u7684\u7ffb\u8f66\u7ecf\u5386&#xff1a;\u7b2c\u4e00\u6b21\u7528 sed \u6539\u914d\u7f6e\u6587\u4ef6&#xff0c;\u6211\u4ee5\u4e3a\u50cf vim \u4e00\u6837\u6539\u5b8c\u5c31\u4fdd\u5b58\u4e86&#xff0c;\u7ed3\u679c\u6572\u5b8c sed &#039;s\/8080\/9090\/&#039; nginx.conf&#xff0c;\u4e00\u770b\u6587\u4ef6\u8fd8\u662f 8080&#xff0c;\u6123\u4e86\u534a\u5929\u3002\u8865\u4e0a -i \u624d\u7b97\u6539\u6389\u3002\u540e\u6765\u66f4\u60e8\u7684\u4e00\u6b21&#xff0c;-i \u6539\u5b8c\u53d1\u73b0\u6539\u9519\u4e86&#xff0c;\u53c8\u6ca1\u5907\u4efd&#xff0c;\u53ea\u80fd\u9760\u5feb\u7167\u56de\u6eda\u3002<\/p>\n<p>\u6240\u4ee5\u73b0\u5728\u517b\u6210\u4e60\u60ef&#xff1a;\u7528 -i \u4e4b\u524d\u5148 -i.bak \u5907\u4efd\u4e00\u4efd&#xff0c;\u6216\u8005\u5148\u4e0d\u52a0 -i \u8dd1\u4e00\u904d\u770b\u7ed3\u679c\u5bf9\u4e0d\u5bf9&#xff0c;\u786e\u8ba4\u6ca1\u95ee\u9898\u518d\u4e0a -i\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u5751&#xff1a;\u8def\u5f84\u91cc\u5e26\u659c\u6760\u3002\u66ff\u6362\u8def\u5f84\u65f6&#xff0c;\u6bd4\u5982\u8981\u628a \/usr\/local \u6362\u6210 \/opt&#xff0c;\u76f4\u63a5\u5199 sed &#039;s\/\/usr\/local\/\/opt\/&#039; \u5c31\u5168\u4e71\u4e86&#xff0c;\u56e0\u4e3a \/ \u65e2\u662f\u5b9a\u754c\u7b26\u53c8\u662f\u5185\u5bb9\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u6362\u4e2a\u5b9a\u754c\u7b26&#xff0c;\u6bd4\u5982\u7528 |&#xff1a;<\/p>\n<p><span class=\"token function\">sed<\/span> <span class=\"token string\">&#039;s|\/usr\/local|\/opt|g&#039;<\/span> <span class=\"token function\">file<\/span><\/p>\n<p>\u8fd9\u62db\u540e\u6765\u5e2e\u6211\u7701\u4e86\u5f88\u591a\u4e8b\u3002<\/p>\n<hr \/>\n<h3>3. awk\u2014\u2014\u628a\u6587\u672c\u5f53\u8868\u683c&#xff0c;\u6309\u5217\u5904\u7406<\/h3>\n<p>awk \u662f\u4e09\u4e2a\u91cc\u6700&#034;\u91cd&#034;\u7684\u4e00\u4e2a&#xff0c;\u5b83\u5176\u5b9e\u662f\u4e00\u95e8\u5c0f\u8bed\u8a00\u3002\u4f46\u5728\u5b66\u751f\u9636\u6bb5&#xff0c;\u6211\u4eec\u4e3b\u8981\u7528\u5b83\u505a\u4e24\u4ef6\u4e8b&#xff1a;\u6309\u5217\u53d6\u5b57\u6bb5 \u548c \u6309\u6761\u4ef6\u7b97\u6570\u3002<\/p>\n<p>\u540d\u5b57\u6765\u6e90\u633a\u6709\u610f\u601d&#xff0c;\u662f\u4e09\u4e2a\u4f5c\u8005\u7684\u9996\u5b57\u6bcd&#xff1a;Aho\u3001Weinberger\u3001Kernighan\u3002<\/p>\n<h4>\u6309\u5217\u53d6\u5b57\u6bb5<\/h4>\n<p>awk \u9ed8\u8ba4\u6309&#034;\u8fde\u7eed\u7a7a\u767d&#034;&#xff08;\u7a7a\u683c\/\u5236\u8868\u7b26&#xff09;\u628a\u6bcf\u884c\u62c6\u6210\u5b57\u6bb5&#xff0c;$1 \u662f\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u3001$2 \u662f\u7b2c\u4e8c\u4e2a\u2026\u2026$0 \u662f\u6574\u884c&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5047\u8bbe ps aux \u7684\u8f93\u51fa&#xff1a;USER PID %CPU %MEM &#8230;<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $1}&#039;<\/span>       <span class=\"token comment\"># \u53ea\u6253\u5370\u7b2c\u4e00\u5217&#xff08;\u7528\u6237\u540d&#xff09;<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $2}&#039;<\/span>       <span class=\"token comment\"># \u53ea\u6253\u5370\u7b2c\u4e8c\u5217&#xff08;PID&#xff09;<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $1, $2}&#039;<\/span>   <span class=\"token comment\"># \u6253\u5370\u7b2c\u4e00\u3001\u4e8c\u5217<\/span><\/p>\n<p>$NF \u8868\u793a\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5&#xff08;NF &#061; Number of Fields&#xff0c;\u5b57\u6bb5\u4e2a\u6570&#xff09;&#xff0c;\u6240\u4ee5 $NF \u5c31\u662f&#034;\u7b2c NF \u4e2a\u5b57\u6bb5&#034;\u3002<\/p>\n<p><span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $NF}&#039;<\/span> <span class=\"token function\">file<\/span>          <span class=\"token comment\"># \u6253\u5370\u6bcf\u884c\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5<\/span><\/p>\n<p>\u5751&#xff1a;$NF \u548c NF \u5206\u4e0d\u6e05\u3002 \u6211\u7b2c\u4e00\u6b21\u770b awk &#039;{print $NF}&#039; \u60f3\u4e86\u534a\u5929\u3002$NF \u662f&#034;\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u7684\u5185\u5bb9&#034;&#xff0c;\u800c\u88f8\u7684 NF \u662f&#034;\u8fd9\u4e00\u884c\u603b\u5171\u6709\u51e0\u4e2a\u5b57\u6bb5&#034;\u3002\u4e00\u4e2a\u662f\u5185\u5bb9&#xff0c;\u4e00\u4e2a\u662f\u6570\u5b57&#xff0c;\u505a\u9898\u7684\u65f6\u5019\u7ecf\u5e38\u5728\u8fd9\u7ffb\u8f66\u3002<\/p>\n<h4>\u6307\u5b9a\u5206\u9694\u7b26&#xff1a;-F<\/h4>\n<p>\u5904\u7406 \/etc\/passwd \u8fd9\u79cd\u7528\u5192\u53f7\u5206\u9694\u7684\u6587\u4ef6&#xff0c;\u9ed8\u8ba4\u6309\u7a7a\u683c\u62c6\u5c31\u5e9f\u4e86&#xff0c;\u8981\u7528 -F \u6307\u5b9a&#xff1a;<\/p>\n<p><span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;{print $1}&#039;<\/span> \/etc\/passwd      <span class=\"token comment\"># \u6253\u5370\u6240\u6709\u7528\u6237\u540d<\/span><br \/>\n<span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;{print $1, $7}&#039;<\/span> \/etc\/passwd  <span class=\"token comment\"># \u7528\u6237\u540d &#043; \u767b\u5f55 shell<\/span><br \/>\n<span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;{print NF}&#039;<\/span> \/etc\/passwd      <span class=\"token comment\"># \u6bcf\u884c\u6709\u51e0\u4e2a\u5b57\u6bb5&#xff08;passwd \u662f 7 \u4e2a&#xff09;<\/span><\/p>\n<h4>\u5185\u7f6e\u53d8\u91cf NR \u548c NF<\/h4>\n<ul>\n<li>NR &#061; Number of Record&#xff0c;\u5904\u7406\u5230\u7b2c\u51e0\u884c\u4e86&#xff08;\u884c\u53f7&#xff09;<\/li>\n<li>NF &#061; Number of Field&#xff0c;\u5f53\u524d\u884c\u7684\u5b57\u6bb5\u4e2a\u6570<\/li>\n<\/ul>\n<p><span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;NR&#061;&#061;1 {print $0}&#039;<\/span> \/etc\/passwd    <span class=\"token comment\"># \u6253\u5370\u7b2c\u4e00\u884c<\/span><br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print NR, $1}&#039;<\/span> <span class=\"token function\">file<\/span>                 <span class=\"token comment\"># \u7ed9\u6bcf\u884c\u52a0\u4e0a\u884c\u53f7<\/span><\/p>\n<h4>\u6761\u4ef6\u7b5b\u9009\u548c\u8ba1\u7b97<\/h4>\n<p>awk \u540e\u9762\u53ef\u4ee5\u8ddf\u6761\u4ef6&#xff0c;\u6761\u4ef6\u6210\u7acb\u624d\u6267\u884c {} \u91cc\u7684\u52a8\u4f5c&#xff1a;<\/p>\n<p><span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;$3 &gt;&#061; 1000 {print $1}&#039;<\/span> \/etc\/passwd    <span class=\"token comment\"># UID \u5927\u4e8e\u7b49\u4e8e 1000 \u7684\u7528\u6237<\/span><br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$3 &gt; 50 {print $1, $3}&#039;<\/span> stats.txt         <span class=\"token comment\"># \u7b2c\u4e09\u5217\u5927\u4e8e 50 \u7684\u884c<\/span><br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;NR &gt; 1 {sum &#043;&#061; $5} END {print sum}&#039;<\/span> <span class=\"token function\">file<\/span>  <span class=\"token comment\"># \u4ece\u7b2c 2 \u884c\u5f00\u59cb\u7d2f\u52a0\u7b2c 5 \u5217<\/span><\/p>\n<p>BEGIN \u548c END \u662f\u4e24\u4e2a\u7279\u6b8a\u5757&#xff1a;BEGIN \u5728\u8bfb\u6587\u4ef6\u524d\u6267\u884c&#xff08;\u5e38\u7528\u6765\u6253\u8868\u5934&#xff09;&#xff0c;END \u5728\u5904\u7406\u5b8c\u6240\u6709\u884c\u540e\u6267\u884c&#xff08;\u5e38\u7528\u6765\u8f93\u51fa\u6c47\u603b&#xff09;&#xff1a;<\/p>\n<p><span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;BEGIN {print &#034;\u7528\u6237\u540d\\\\tShell&#034;} {print $1&#034;\\\\t&#034;$7} END {print &#034;\u7edf\u8ba1\u5b8c\u6bd5&#034;}&#039;<\/span> \/etc\/passwd<\/p>\n<h4>awk \u7684\u4e00\u4e2a\u5b8c\u6574\u5b9e\u6218<\/h4>\n<p>\u8001\u5e08\u7559\u7684\u4f5c\u4e1a&#xff1a;\u7edf\u8ba1\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u91cc\u6bcf\u4e2a IP \u51fa\u73b0\u4e86\u591a\u5c11\u6b21&#xff08;\u7c7b\u4f3c\u770b\u8c01\u5728\u5237\u670d\u52a1\u5668&#xff09;\u3002<\/p>\n<p><span class=\"token comment\"># \u5047\u8bbe\u65e5\u5fd7\u6bcf\u884c\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u662f IP<\/span><br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $1}&#039;<\/span> access.log <span class=\"token operator\">|<\/span> <span class=\"token function\">sort<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">uniq<\/span> <span class=\"token parameter variable\">-c<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">sort<\/span> <span class=\"token parameter variable\">-rn<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-10<\/span><\/p>\n<p>\u62c6\u89e3\u4e00\u4e0b&#xff1a;<\/p>\n<ul>\n<li>awk &#039;{print $1}&#039; \u2014 \u628a\u6bcf\u884c\u7684 IP \u62a0\u51fa\u6765<\/li>\n<li>sort \u2014 \u6392\u5e8f&#xff08;uniq \u53ea\u80fd\u7edf\u8ba1\u76f8\u90bb\u7684\u91cd\u590d\u9879&#xff0c;\u5fc5\u987b\u5148 sort&#xff09;<\/li>\n<li>uniq -c \u2014 \u7edf\u8ba1\u6bcf\u4e2a IP \u51fa\u73b0\u6b21\u6570<\/li>\n<li>sort -rn \u2014 \u6309\u6b21\u6570\u4ece\u5927\u5230\u5c0f\u6392<\/li>\n<li>head -10 \u2014 \u53d6\u524d 10 \u540d<\/li>\n<\/ul>\n<p>\u6211\u7b2c\u4e00\u6b21\u5199\u7684\u65f6\u5019\u6f0f\u4e86 sort&#xff0c;\u7ed3\u679c uniq -c \u7edf\u8ba1\u51fa\u6765\u5168\u662f 1&#xff0c;\u4ee5\u4e3a\u65e5\u5fd7\u6709\u95ee\u9898\u3002\u540e\u6765\u624d\u77e5\u9053 uniq \u53ea\u5408\u5e76\u76f8\u90bb\u7684\u884c&#xff0c;\u4e0d\u6392\u5e8f\u5c31\u7edf\u8ba1\u7b49\u4e8e\u767d\u7edf\u8ba1\u3002<\/p>\n<h4>awk \u7684\u5751<\/h4>\n<p>\u57511&#xff1a;\u5355\u5f15\u53f7\u4e0d\u80fd\u5c11\u3002 awk \u7684\u7a0b\u5e8f\u90e8\u5206\u8981\u5305\u5728\u5355\u5f15\u53f7\u91cc&#xff0c;awk &#039;{print $1}&#039;\u3002\u6211\u8bd5\u8fc7\u7528\u53cc\u5f15\u53f7 awk &#034;{print $1}&#034;&#xff0c;\u7ed3\u679c $1 \u88ab shell \u5148\u89e3\u6790\u6210\u7a7a\u5b57\u7b26\u4e32\u4e86&#xff0c;\u6253\u5370\u51fa\u6765\u5168\u662f\u7a7a\u884c\u3002awk \u91cc\u7684 $ \u8981\u4ea4\u7ed9 awk&#xff0c;\u522b\u8ba9 shell \u5403\u6389\u3002<\/p>\n<p>\u57512&#xff1a;\u9ed8\u8ba4\u5206\u9694\u7b26\u662f\u8fde\u7eed\u7a7a\u767d\u3002 \u5904\u7406 root:x:0:0 \u8fd9\u79cd\u6ca1\u6709\u7a7a\u683c\u7684\u6587\u672c&#xff0c;\u4e0d\u52a0 -F: \u53ea\u4f1a\u5f97\u5230\u4e00\u6574\u5757&#xff0c;\u5b57\u6bb5\u6839\u672c\u6ca1\u5207\u5f00\u3002<\/p>\n<p>\u57513&#xff1a;print \u91cc\u9017\u53f7\u662f\u6709\u610f\u4e49\u7684\u3002 print $1, $2 \u4e24\u4e2a\u5b57\u6bb5\u4e4b\u95f4\u4f1a\u6709\u4e00\u4e2a\u7a7a\u683c&#xff08;\u9ed8\u8ba4\u5206\u9694\u7b26&#xff09;&#xff0c;\u800c print $1 $2 \u662f\u76f4\u63a5\u62fc\u5728\u4e00\u8d77\u3002\u6211\u7b2c\u4e00\u6b21\u6ca1\u6ce8\u610f&#xff0c;\u8f93\u51fa\u6324\u6210\u4e00\u56e2\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u5251\u5ba2\u8fde\u62db&#xff1a;\u4e00\u6761\u547d\u4ee4\u641e\u5b9a\u4e00\u4ef6\u4e8b<\/h3>\n<p>\u5355\u72ec\u7528\u6bcf\u4e2a\u90fd\u80fd\u7528&#xff0c;\u4f46\u771f\u6b63\u7684\u5a01\u529b\u662f\u628a\u5b83\u4eec\u4e32\u8d77\u6765\u3002\u5206\u4eab\u4e24\u4e2a\u6211\u7ec3\u8fc7\u7684\u5b8c\u6574\u6848\u4f8b&#xff1a;<\/p>\n<p>\u9700\u6c42&#xff1a;\u627e\u7cfb\u7edf\u91cc\u6240\u6709\u80fd\u767b\u5f55\u7684\u7528\u6237&#xff0c;\u770b\u770b\u4ed6\u4eec\u7684 shell \u6b63\u4e0d\u6b63\u5e38\u3002<\/p>\n<p><span class=\"token comment\"># 1. grep \u8fc7\u6ee4\u6389\u4e0d\u80fd\u767b\u5f55\u7684&#xff08;shell \u662f nologin \u7684&#xff09;<\/span><br \/>\n<span class=\"token comment\"># 2. awk \u53d6\u7528\u6237\u540d\u548c shell<\/span><br \/>\n<span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-v<\/span> <span class=\"token string\">&#034;\/sbin\/nologin&#034;<\/span> \/etc\/passwd <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> -F: <span class=\"token string\">&#039;{print $1, $7}&#039;<\/span><\/p>\n<p>\u9700\u6c42&#xff1a;\u7edf\u8ba1\u5f53\u524d\u7cfb\u7edf\u5185\u5b58\u5360\u7528\u6700\u9ad8\u7684\u51e0\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p><span class=\"token comment\"># ps aux \u7684\u5217&#xff1a;USER PID %CPU %MEM VSZ RSS &#8230; COMMAND<\/span><br \/>\n<span class=\"token comment\"># sort \u6309\u7b2c 4 \u5217&#xff08;%MEM&#xff09;\u4ece\u5927\u5230\u5c0f\u6392&#xff0c;\u518d awk \u53d6 PID\u3001\u5185\u5b58\u3001\u547d\u4ee4<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">sort<\/span> <span class=\"token parameter variable\">-rk4<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-5<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $2, $4, $11}&#039;<\/span><\/p>\n<p>\u9700\u6c42&#xff1a;\u770b\u770b\u914d\u7f6e\u6587\u4ef6\u91cc\u6ce8\u91ca\u5199\u4e86\u591a\u5c11\u884c\u3002<\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;^#&#034;<\/span> \/etc\/ssh\/sshd_config <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span><br \/>\n<span class=\"token comment\"># \u6216\u8005\u7528 awk \u6570<\/span><br \/>\n<span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;\/^#\/ {count&#043;&#043;} END {print count}&#039;<\/span> \/etc\/ssh\/sshd_config<\/p>\n<p>\u7ec3\u8fde\u62db\u7684\u6700\u5927\u611f\u53d7\u662f&#xff1a;\u6bcf\u6761\u547d\u4ee4\u90fd\u53ea\u505a\u4e00\u4ef6\u4e8b&#xff0c;\u9760\u7ba1\u9053\u628a\u5b83\u4eec\u62fc\u8d77\u6765\u3002\u8fd9\u8ddf\u6211\u4e4b\u524d\u5728 [[7.22 \u521b\u5efa\u3001\u67e5\u627e\u548c\u7f16\u8f91\u6587\u672c\u6587\u4ef6]] \u5b66\u7684\u7ba1\u9053\u662f\u540c\u4e00\u4ef6\u4e8b\u2014\u2014\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u51fa&#xff0c;\u662f\u4e0b\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u5165\u3002<\/p>\n<hr \/>\n<h3>\u6211\u8e29\u8fc7\u7684\u5751\u5408\u96c6&#xff08;\u8003\u524d\u5fc5\u770b&#xff09;<\/h3>\n<table>\n<tr>\u5751\u6211\u7684\u7ffb\u8f66\u7ecf\u5386\u6b63\u786e\u505a\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>grep \u5339\u914d\u5230\u81ea\u5df1<\/td>\n<td>ps aux | grep sshd \u7ed3\u679c\u91cc\u6df7\u8fdb grep \u81ea\u5df1<\/td>\n<td>\u52a0 grep -v grep&#xff0c;\u6216\u76f4\u63a5 pgrep sshd<\/td>\n<\/tr>\n<tr>\n<td>\u57fa\u672c\u6b63\u5219 \/ \u6269\u5c55\u6b63\u5219<\/td>\n<td>grep &#034;(a|b)&#034; \u4e0d\u751f\u6548<\/td>\n<td>\u7528 grep -E \u6216 egrep<\/td>\n<\/tr>\n<tr>\n<td>sed \u5fd8\u8bb0 -i<\/td>\n<td>\u8dd1\u5b8c sed &#039;s\/x\/y\/&#039; \u6587\u4ef6\u6ca1\u53d8<\/td>\n<td>\u786e\u8ba4\u65e0\u8bef\u540e\u52a0 -i&#xff0c;\u6700\u597d -i.bak \u5907\u4efd<\/td>\n<\/tr>\n<tr>\n<td>sed \u66ff\u6362\u8def\u5f84\u659c\u6760\u6df7\u4e71<\/td>\n<td>\u66ff\u6362\u5e26 \/ \u7684\u8def\u5f84\u5199\u9519<\/td>\n<td>\u7528 &#096;<\/td>\n<\/tr>\n<tr>\n<td>sed \u4e0d\u52a0 g<\/td>\n<td>\u6bcf\u884c\u53ea\u66ff\u6362\u7b2c\u4e00\u4e2a\u5339\u914d<\/td>\n<td>\u5168\u66ff\u6362\u8981\u52a0 g<\/td>\n<\/tr>\n<tr>\n<td>awk \u7528\u4e86\u53cc\u5f15\u53f7<\/td>\n<td>$1 \u88ab shell \u5403\u6389&#xff0c;\u8f93\u51fa\u5168\u662f\u7a7a<\/td>\n<td>awk \u7a0b\u5e8f\u5fc5\u987b\u7528\u5355\u5f15\u53f7\u5305<\/td>\n<\/tr>\n<tr>\n<td>awk \u5fd8\u4e86 -F<\/td>\n<td>\u5904\u7406 passwd \u53ea\u80fd\u53d6\u5230\u4e00\u6574\u5757<\/td>\n<td>\u5192\u53f7\u5206\u9694\u7528 -F:<\/td>\n<\/tr>\n<tr>\n<td>uniq \u524d\u6ca1 sort<\/td>\n<td>\u7edf\u8ba1\u7ed3\u679c\u5168\u662f 1<\/td>\n<td>uniq -c \u524d\u5148 sort<\/td>\n<\/tr>\n<tr>\n<td>$NF \u548c NF \u6df7\u6dc6<\/td>\n<td>\u60f3\u53d6\u6700\u540e\u4e00\u5217\u5374\u5199\u6210 {print NF}<\/td>\n<td>\u53d6\u5185\u5bb9\u7528 $NF&#xff0c;\u53d6\u6570\u91cf\u7528 NF<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u901f\u8bb0\u6e05\u5355<\/h4>\n<p>grep \u7ba1\u627e\u884c&#xff0c;sed \u7ba1\u6539\u5185\u5bb9&#xff0c;awk \u7ba1\u53d6\u5217\/\u7b97<br \/>\ngrep&#xff1a;-i \u5ffd\u7565\u5927\u5c0f\u5199&#xff0c;-v \u53cd\u5411&#xff0c;-E \u6269\u5c55\u6b63\u5219&#xff0c;-o \u53ea\u8f93\u51fa\u5339\u914d<br \/>\nsed&#xff1a;s\/\u65e7\/\u65b0\/g \u66ff\u6362&#xff0c;-n \u914d p \u53ea\u6253\u5370&#xff0c;-i \u624d\u771f\u6b63\u6539\u6587\u4ef6<br \/>\nawk&#xff1a;\u9ed8\u8ba4\u6309\u7a7a\u767d\u62c6\u5217&#xff0c;-F: \u6307\u5b9a\u5192\u53f7\u5206\u9694<br \/>\nawk&#xff1a;$1 \u53d6\u7b2c 1 \u5217&#xff0c;$NF \u53d6\u6700\u540e\u4e00\u5217&#xff0c;NR \u662f\u884c\u53f7&#xff0c;NF \u662f\u5217\u6570<br \/>\nuniq -c \u4e4b\u524d\u5fc5\u987b sort<\/p>\n<hr \/>\n<h3>\u4e0b\u4e00\u6b65\u6253\u7b97<\/h3>\n<ul>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u60f3\u518d\u7cfb\u7edf\u7ec3\u4e00\u904d&#xff0c;grep\u3001sed\u3001awk \u90fd\u79bb\u4e0d\u5f00\u5b83<\/li>\n<li>\u5b66\u5b66 awk \u7684 if\u3001for \u8fd9\u4e9b\u8bed\u6cd5&#xff0c;\u5b83\u5176\u5b9e\u662f\u95e8\u5b8c\u6574\u7684\u7f16\u7a0b\u8bed\u8a00<\/li>\n<li>\u60f3\u8bd5\u7740\u5199\u4e00\u4e2a\u771f\u6b63\u80fd\u7528\u7684\u65e5\u5fd7\u5206\u6790\u5c0f\u811a\u672c&#xff0c;\u628a\u4e09\u5251\u5ba2 &#043; \u7ba1\u9053\u7528\u719f<\/li>\n<\/ul>\n<hr \/>\n<h3>\u6700\u540e\u8bf4\u4e24\u53e5<\/h3>\n<p>\u5b66\u5b8c\u4e09\u5251\u5ba2&#xff0c;\u6700\u5927\u7684\u611f\u53d7\u662f&#xff0c;\u4ee5\u524d\u89c9\u5f97&#034;\u9ad8\u6df1&#034;\u7684\u65e5\u5fd7\u5904\u7406\u547d\u4ee4&#xff0c;\u5176\u5b9e\u5c31\u662f\u628a\u4e09\u4e2a\u5404\u53f8\u5176\u804c\u7684\u5c0f\u5de5\u5177\u7528\u7ba1\u9053\u62fc\u8d77\u6765&#xff1a;grep \u627e\u3001sed \u6539\u3001awk \u53d6&#xff0c;\u5404\u5e72\u5404\u7684&#xff0c;\u7ec4\u5408\u8d77\u6765\u80fd\u5e72\u633a\u591a\u4e8b\u3002<\/p>\n<p>\u4e0d\u8fc7\u8bf4\u5b9e\u8bdd&#xff0c;awk \u6211\u73b0\u5728\u8fd8\u662f\u7528\u5f97\u6700\u751f\u758f&#xff0c;\u5b83\u7684\u8bed\u6cd5\u957f\u5f97\u8ddf C \u6709\u70b9\u50cf&#xff0c;\u4e00\u590d\u6742\u5c31\u72af\u6655&#xff0c;\u4f30\u8ba1\u540e\u9762\u5199\u811a\u672c\u7684\u65f6\u5019\u5f97\u591a\u7ec3\u3002sed \u7684 -i \u6211\u73b0\u5728\u6bcf\u6b21\u7528\u4e4b\u524d\u90fd\u8981\u786e\u8ba4\u4e00\u904d\u6709\u6ca1\u6709\u5907\u4efd\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u5c0f\u4e8b&#xff1a;\u4e09\u5251\u5ba2\u914d\u5408\u6b63\u5219\u7684\u65f6\u5019\u7b26\u53f7\u771f\u7684\u5f88\u591a&#xff0c;^\u3001$\u3001.\u3001*\u3001[] \u90fd\u662f\u5565\u610f\u601d&#xff0c;\u6211\u5230\u73b0\u5728\u5076\u5c14\u8fd8\u8981\u7ffb man grep\u3002\u53cd\u6b63\u8bb0\u4e0d\u4f4f\u5c31\u67e5\u5457&#xff0c;\u7528\u591a\u4e86\u81ea\u7136\u5c31\u719f\u4e86\u3002<\/p>\n<p>\u5199\u8fd9\u7bc7\u7684\u65f6\u5019&#xff0c;\u865a\u62df\u673a\u91cc \/var\/log \u7684\u65e5\u5fd7\u88ab\u6211 grep \/ sed \/ awk \u6765\u56de\u6298\u817e\u4e86\u4e2a\u904d&#xff0c;\u8fd8\u597d\u6709\u5feb\u7167&#xff0c;\u73a9\u574f\u4e86\u968f\u65f6\u56de\u6eda\u3002<\/p>\n<hr \/>\n<p>2026\u5e748\u6708 \u00b7 \u5199\u4e8e shell \u7f16\u7a0b\u8bfe\u540e \u00b7 \u865a\u62df\u673a\u91cc \/var\/log \u7684\u65e5\u5fd7\u88ab\u6211\u7ffb\u4e86\u4e2a\u5e95\u671d\u5929<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shell \u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97&#xff0c;\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11<br \/>\n\u5f00\u5934<br \/>\n\u5b66 Linux \u5230\u8fd9\u4f1a\u513f&#xff0c;\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u670d\u52a1\u63a7\u5236\u8fd9\u4e9b\u6211\u591a\u5c11\u90fd\u80fd\u4e0a\u624b\u4e86\u3002\u4f46\u6bcf\u6b21\u770b\u7f51\u4e0a\u6559\u7a0b\u91cc\u90a3\u79cd\\&#8221;\u4e00\u6761\u547d\u4ee4\u5904\u7406\u65e5\u5fd7\\&#8221;\u7684\u5199\u6cd5\u2014\u2014cat xxx.log | grep \u62a5\u9519 | awk {print $2} | sort | uniq -c\u2014\u2014\u6211\u5c31\u5934\u76ae\u53d1\u9ebb&#xff0c;\u611f\u89c9\u50cf\u5728\u770b\u5916\u661f\u6587\u3002<br \/>\n\u8fd9\u5468\u8001\u5e08\u6b63\u5f0f\u8bb2\u4e86\u4f20\u8bf4\u4e2d\u7684\u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001aw<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,763,43,44],"topic":[],"class_list":["post-89997","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-763","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \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\/89997.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Shell \u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97&#xff0c;\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 \u5f00\u5934 \u5b66 Linux \u5230\u8fd9\u4f1a\u513f&#xff0c;\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u670d\u52a1\u63a7\u5236\u8fd9\u4e9b\u6211\u591a\u5c11\u90fd\u80fd\u4e0a\u624b\u4e86\u3002\u4f46\u6bcf\u6b21\u770b\u7f51\u4e0a\u6559\u7a0b\u91cc\u90a3\u79cd&quot;\u4e00\u6761\u547d\u4ee4\u5904\u7406\u65e5\u5fd7&quot;\u7684\u5199\u6cd5\u2014\u2014cat xxx.log | grep \u62a5\u9519 | awk {print $2} | sort | uniq -c\u2014\u2014\u6211\u5c31\u5934\u76ae\u53d1\u9ebb&#xff0c;\u611f\u89c9\u50cf\u5728\u770b\u5916\u661f\u6587\u3002 \u8fd9\u5468\u8001\u5e08\u6b63\u5f0f\u8bb2\u4e86\u4f20\u8bf4\u4e2d\u7684\u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001aw\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/89997.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-04T05:09:16+00:00\" \/>\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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89997.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/89997.html\",\"name\":\"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-04T05:09:16+00:00\",\"dateModified\":\"2026-08-04T05:09:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/89997.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/89997.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89997.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11\"}]},{\"@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":"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \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\/89997.html","og_locale":"zh_CN","og_type":"article","og_title":"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Shell \u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97&#xff0c;\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 \u5f00\u5934 \u5b66 Linux \u5230\u8fd9\u4f1a\u513f&#xff0c;\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u670d\u52a1\u63a7\u5236\u8fd9\u4e9b\u6211\u591a\u5c11\u90fd\u80fd\u4e0a\u624b\u4e86\u3002\u4f46\u6bcf\u6b21\u770b\u7f51\u4e0a\u6559\u7a0b\u91cc\u90a3\u79cd\"\u4e00\u6761\u547d\u4ee4\u5904\u7406\u65e5\u5fd7\"\u7684\u5199\u6cd5\u2014\u2014cat xxx.log | grep \u62a5\u9519 | awk {print $2} | sort | uniq -c\u2014\u2014\u6211\u5c31\u5934\u76ae\u53d1\u9ebb&#xff0c;\u611f\u89c9\u50cf\u5728\u770b\u5916\u661f\u6587\u3002 \u8fd9\u5468\u8001\u5e08\u6b63\u5f0f\u8bb2\u4e86\u4f20\u8bf4\u4e2d\u7684\u4e09\u5251\u5ba2&#xff1a;grep\u3001sed\u3001aw","og_url":"https:\/\/www.wsisp.com\/helps\/89997.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-04T05:09:16+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/89997.html","url":"https:\/\/www.wsisp.com\/helps\/89997.html","name":"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-04T05:09:16+00:00","dateModified":"2026-08-04T05:09:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/89997.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/89997.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/89997.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Shell \u4e09\u5251\u5ba2\uff1agrep\u3001sed\u3001awk\u2014\u2014\u627e\u3001\u6539\u3001\u7b97\uff0c\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11"}]},{"@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\/89997","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=89997"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89997\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=89997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=89997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=89997"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=89997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}