{"id":409,"date":"2024-10-09T23:28:41","date_gmt":"2024-10-09T15:28:41","guid":{"rendered":"https:\/\/cococat.top\/?p=409"},"modified":"2026-04-01T22:24:47","modified_gmt":"2026-04-01T14:24:47","slug":"%e5%91%bd%e4%bb%a4%e8%a1%8c%e6%95%88%e7%8e%87%e6%9d%82%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/10\/09\/%e5%91%bd%e4%bb%a4%e8%a1%8c%e6%95%88%e7%8e%87%e6%9d%82%e8%ae%b0\/","title":{"rendered":"\u547d\u4ee4\u884c\u6548\u7387\u6742\u8bb0"},"content":{"rendered":"<h2>1. \u6392\u5e8f+\u7edf\u8ba1<\/h2>\n<pre><code>sort | uniq -c<\/code><\/pre>\n<p>\u6ce8\u610f <code>uniq -c<\/code> \u53ea\u80fd\u7edf\u8ba1\u4e34\u8fd1\u7684\u91cd\u590d\u884c\uff0c\u56e0\u6b64\u9700\u8981 sort<\/p>\n<h2>2. grep<\/h2>\n<ul>\n<li>-v \u6392\u9664<\/li>\n<li>-E \u6b63\u5219<\/li>\n<\/ul>\n<h2>3. awk<\/h2>\n<h3>3.1 NF \u4e0e\u5faa\u73af<\/h3>\n<p><code>NF<\/code> \u662f awk \u8bed\u6cd5\u5185\u7f6e\u53d8\u91cf\uff0c\u4ee3\u8868 field \u7684\u4e2a\u6570\u3002 \u53ef\u4ee5\u914d\u5408\u5faa\u73af\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>echo &quot;a bb ccc&quot; | awk &#039;{ for (i=1; i&lt;=NF; i++) print $i}&#039;<\/code><\/pre>\n<p>\u4f1a\u8f93\u51fa<\/p>\n<pre><code>a\nbb\nccc<\/code><\/pre>\n<h3>3.2 \u6307\u5b9a\u5206\u5272\u7b26<\/h3>\n<p><code>-F<\/code> \u6307\u5b9a\u5206\u5272 field \u7684\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>echo &quot;a&amp;bb&amp;ccc&quot; | awk -F &#039;&amp;&#039; &#039;{ for (i=1; i&lt;=NF; i++) print $i}&#039;<\/code><\/pre>\n<h3>3.3 \u63d0\u53d6\u6307\u5b9a\u503c\u5e76\u8ba1\u7b97\u5e73\u5747<\/h3>\n<p>\u5047\u8bbe\u67d0\u6587\u4ef6\u91cc\u5305\u542b\u4e86\u4e00\u4e9b log\uff0c\u90fd\u542b\u6709 &quot;xxms,&quot; \u7684\u683c\u5f0f\u3002\u5219\uff1a<\/p>\n<pre><code>cat 1.log  | awk &#039;{ total += $NF; count++ } END { if (count &gt; 0) print &quot;Average:&quot;, total\/count, &quot;ms&quot;; }&#039; RS=&quot;ms,&quot;<\/code><\/pre>\n<p>\u4e00\u4e9b\u8981\u70b9\uff1a<\/p>\n<ul>\n<li><code>$NF<\/code>: \u8fd9\u4e2a\u5199\u6cd5\u6d3b\u7528\u4e86 3.1 \u8282\u63d0\u5230\u7684 <code>NF<\/code>\uff0c<code>NF<\/code> \u4ee3\u8868 field \u4e2a\u6570\uff0c\u56e0\u6b64 <code>$NF<\/code> \u4ee3\u8868\u6bcf\u884c\u6700\u540e\u4e00\u4e2a field \u7684\u503c<\/li>\n<li><code>RS=&quot;ms,&quot;<\/code>: <code>RS<\/code> \u6307\u5b9a\u5206\u9694 record \u7684\u7b26\u53f7\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b <code>RS<\/code> \u662f\u6362\u884c\u7b26\u3002\u5728\u672c\u8282\u7684\u573a\u666f\u4e2d\uff0c\u5728\u6307\u5b9a\u5206\u9694\u7b26\u4e3a <code>ms,<\/code> \u540e\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>$NF<\/code> \u76f4\u63a5\u83b7\u53d6\u6211\u4eec\u60f3\u8981\u7684\u6570\u503c\uff0c\u662f\u4e00\u79cd\u53d6\u5de7\u7684\u65b9\u6848\u3002<\/li>\n<\/ul>\n<h3>3.4 NR<\/h3>\n<p>\u7528\u4e8e\u6bcf\u4e00\u884c\u5904\u7406\uff0c\u548c END \u5757\u4e2d\u3002<\/p>\n<p>\u5728\u9010\u884c\u5904\u7406\u65f6\uff0c\u4ee3\u8868\u5f53\u524d\u884c\u6570\uff0c\u4ece 1 \u5f00\u59cb\uff1b\u5728 END \u5757\u4e2d\uff0c\u4e3a\u6587\u4ef6\u7684\u884c\u6570\u3002<\/p>\n<p>\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u7ec4\u7684\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7cfb\u5217\u8017\u65f6\u6570\u636e\uff0c\u73b0\u5728\u9700\u8981\u63d0\u53d6\u51fa\u5176\u4e2d\u7684 p95\uff0c\u5219\u53ef\u4ee5\u5728 sort \u4e4b\u540e\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-awk\">{ data[NR] = $NF }\nEND {\n    i = int(NR * 0.95);\n    print &quot;p95 is&quot;, data[i];\n}\n<\/code><\/pre>\n<h2>4. sed<\/h2>\n<h3>4.1 \u5b57\u7b26\u66ff\u6362<\/h3>\n<p>\u4f8b\u5982\u628a <code>orange<\/code> \u66ff\u6362\u4e3a <code>apple<\/code>\uff0c\u90a3\u4e48\uff1a<\/p>\n<pre><code>echo &quot;I love orange&quot; | sed &#039;s\/orange\/apple\/g&#039;<\/code><\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>\/<\/code>: \u662f sed \u7684\u8bed\u6cd5\u5206\u9694\u7b26\uff0c\u5206\u6210\u4e86 <code>s<\/code>\u3001<code>orange<\/code>\u3001<code>apple<\/code>\u3001<code>g<\/code> \u56db\u4e2a\u90e8\u5206<\/li>\n<li><code>s<\/code>: \u6307\u8fdb\u884c\u66ff\u6362\u64cd\u4f5c\uff0cs is short for substitute<\/li>\n<li><code>orange<\/code>\/ <code>apple<\/code>: \u66ff\u6362\u5bf9\u8c61\u548c\u9700\u8981\u66ff\u6362\u4e3a\u7684\u503c<\/li>\n<li><code>g<\/code>: \u5168\u5c40\u66ff\u6362\uff0cg for global\u3002\u5982\u679c\u4e0d\u5e26g\uff0c\u5219\u53ea\u4f1a\u66ff\u6362\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684 orange<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u628a\u66ff\u6362\u503c\u8bbe\u4e3a\u7a7a\uff0c\u4ece\u800c\u4ece\u5b57\u7b26\u6d41\u4e2d\u5220\u9664\u6389\u67d0\u4e2a\u8bcd\u3002\u4f8b\u5982\u9700\u8981\u5904\u7406\u8f6c\u4e49\u5b57\u7b26\u65f6\uff0c\u5982\u679c\u60c5\u51b5\u5141\u8bb8\uff0c\u6211\u4eec\u53ef\u4ee5\u7c97\u66b4\u5730\u5c06\u6240\u6709 <code>\\<\/code> \u5220\u9664\uff0c\u5373\uff1a<\/p>\n<pre><code> echo &#039;{name: alice}&#039; | sed &#039;s\/\\\\\/\/g&#039;<\/code><\/pre>\n<h3>4.2 \u8fdb\u9636\u7528\u6cd5\u4e4b\u5339\u914d\u4e0e\u63d0\u53d6<\/h3>\n<p>\u5728\u5339\u914d\u89c4\u5219\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5728\u66ff\u6362\u5bf9\u8c61\u4e2d\u53ef\u4ee5\u4f7f\u7528 <code>\\1<\/code> \u6765\u8f93\u51fa\u7b2c 1 \u4e2a\u5339\u914d\u9879\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u8fd9\u6837\u7684\u4e00\u4e9b\u6570\u636e\uff1a<\/p>\n<pre><code>&quot;Alice&quot;: 1\n&quot;Bob&quot;: 2<\/code><\/pre>\n<p>\u5219\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>sed &#039;s\/^&quot;\\([^&quot;]*\\).*\/\\1\/&#039;<\/code> \u6765\u63d0\u53d6\u51fa\u5176\u4e2d\u7684 Alice \u548c Bob\uff0c\u4e14\u79fb\u9664\u6389\u4e0d\u5fc5\u8981\u7684\u5f15\u53f7\u3002<\/p>\n<h3>4.3 \u8f93\u51fa\u7279\u5b9a\u884c\u6570<\/h3>\n<p>\u8f93\u51fa\u7b2c 100 ~ 200 \u884c\uff0c\u4e0d\u505a\u4efb\u4f55\u66ff\u6362\uff1a<\/p>\n<pre><code>sed -n &#039;100,200p&#039; my_app.log<\/code><\/pre>\n<p>\u5176\u4e2d -n \u4ee3\u8868\u7981\u7528\u9ed8\u8ba4\u7684\u6bcf\u884c\u90fd\u8f93\u51fa\u7684\u884c\u4e3a\u3002<br \/>\np \u662f print \u5b50\u547d\u4ee4<\/p>\n<p>\u53ea\u8f93\u51fa\u7b2c 100 \u884c\uff0c\u5e76\u4e14\u8fdb\u884c\u66ff\u6362\u7b49\uff1a<\/p>\n<pre><code>sed -n &#039;100{s\/WARN\/WARNING\/g; p;}&#039; my_app.log<\/code><\/pre>\n<h2>5. xargs<\/h2>\n<h3>5.1 \u57fa\u7840\u7528\u6cd5<\/h3>\n<p>-d \u6307\u5b9a\u5206\u9694\u7b26\uff0c-n \u6307\u5b9a\u4e00\u884c\u5c55\u793a\u7684\u4e2a\u6570<\/p>\n<p>\u5b9e\u73b0\u4e00\u884c\u8f6c\u591a\u884c\u3002<\/p>\n<pre><code>echo &#039;1,2,3&#039; | xargs -d, -n1<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\uff1a<\/p>\n<pre><code>1\n2\n3<\/code><\/pre>\n<h3>5.2 \u6307\u5b9a\u5360\u4f4d\u7b26\u5e76\u66ff\u6362<\/h3>\n<p>-I \u6307\u5b9a\u5360\u4f4d\u7b26\uff0c\u5e38\u7528\u7684\u662f <code>{}<\/code>\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u4e0b\u5217\u6587\u4ef6\u90fd\u79fb\u52a8\u8d70\uff1a<\/p>\n<pre><code>find .\/foo -type f -name &quot;*.txt&quot; | xargs -I {} mv {} .\/bar\/<\/code><\/pre>\n<h3>5.3 \u5e76\u53d1\u6267\u884c<\/h3>\n<p><code>-P (max-procs)<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u60f3\u540c\u65f6\u8d77\u597d\u51e0\u4e2a curl \uff0c\u53ef\u4ee5\u8fd9\u4e48\u5e72\uff1a<\/p>\n<pre><code>seq 10 | xargs -P 10 -I {} curl https:\/\/www.a.com<\/code><\/pre>\n<p>\u6ce8\u610f\u8fd9\u91cc -I \u5236\u5b9a\u7684\u5360\u4f4d\u7b26\u5176\u5b9e\u5e76\u6ca1\u6709\u88ab\u7528\u5230\uff0c\u4f46\u662f -I \u9690\u542b\u4e86 -L 1\uff08\u6bcf\u6b21\u53ea\u5904\u7406\u4e00\u884c\uff09\u3002\u5982\u679c\u6ca1\u6709 -I, xargs \u4f1a\u5c1d\u8bd5\u628a seq \u7684\u8f93\u51fa\u5f53\u505a\u53c2\u6570\u6254\u7ed9 curl\uff0c\u5373 <code> curl https:\/\/www.a.com\/api 1 2 3 4 5 6 7 8 9 10<\/code><\/p>\n<p>TODO: \u4e00\u4e9b\u9898\u76ee\uff1a<\/p>\n<ul>\n<li>\u79fb\u9664\u6389\u524d\u5bfc\u3001\u540e\u5bfc\u7684\u591a\u4f59\u7684\u5f15\u53f7<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6392\u5e8f+\u7edf\u8ba1 sort | uniq -c \u6ce8\u610f uniq -c \u53ea\u80fd\u7edf\u8ba1\u4e34\u8fd1\u7684\u91cd\u590d\u884c\uff0c\u56e0\u6b64\u9700\u8981 sort [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,25],"class_list":["post-409","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-terminal"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":7,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":595,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions\/595"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}