{"id":3611,"date":"2017-09-30T10:48:36","date_gmt":"2017-09-30T02:48:36","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3611"},"modified":"2017-09-30T10:48:36","modified_gmt":"2017-09-30T02:48:36","slug":"linux%e4%b8%8bxargs%e5%91%bd%e4%bb%a4%e7%9a%84%e7%94%a8%e6%b3%95%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3611.html","title":{"rendered":"Linux\u4e0bxargs\u547d\u4ee4\u7684\u7528\u6cd5\u5c0f\u7ed3"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"Linux\u4e0bxargs\u547d\u4ee4\u7684\u7528\u6cd5\u5c0f\u7ed3-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u4e0b\u5348\u5728\u5904\u7406\u4e00\u4e9b\u811a\u672c\u95ee\u9898\u7684\u65f6\u5019\u7528\u5230\u4e86xargs\u547d\u4ee4\u7684\u4e00\u4e9b\u4e0d\u90a3\u4e48\u5e38\u7528\u7684\u7528\u6cd5\uff0c\u5728\u89e3\u51b3\u4e86\u5b9e\u9645\u95ee\u9898\u4e4b\u540e\u987a\u4fbf\u603b\u7ed3\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u76f4\u63a5\u53c2\u8003\u3002<\/p>\n<h4 id=\"Linux\u4e0bxargs\u547d\u4ee4\u7684\u7528\u6cd5\u5c0f\u7ed3-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"Linux\u4e0bxargs\u547d\u4ee4\u7684\u7528\u6cd5\u5c0f\u7ed3-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cxargs\u547d\u4ee4\u4f1a\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u4ee5\u7a7a\u683c\u8fde\u63a5\u8d77\u6765\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true \">$ for i in $(seq 1 20); do\r\necho \"line\"$i\r\ndone &gt;&gt;test.txt\r\n\r\n$ cat test.txt\r\nline1\r\nline2\r\n...\r\n\r\n$ cat test.txt | xargs\r\nline1 line2 line3 line4 line5 line6 line7 line8 line9 line10 line11 line12 line13 line14 line15 line16 line17 line18 line19 line20<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4fee\u6539\u4e00\u4e0bxargs\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u6bd4\u5982\u5c06\u300c\u7a7a\u683c\u300d\u6362\u6210\u300c\u5c06\u6bcf\u4e2a\u5143\u7d20\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u518d\u7528\u7a7a\u683c\u8fde\u63a5\u300d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"lang:default decode:true \">$ cat test.txt | xargs -n1 printf '\"%s\" '\r\n\"line1\" \"line2\" \"line3\" \"line4\" \"line5\" \"line6\" \"line7\" \"line8\" \"line9\" \"line10\" \"line11\" \"line12\" \"line13\" \"line14\" \"line15\" \"line16\" \"line17\" \"line18\" \"line19\" \"line20\" %\r\n\r\n$ cat test.txt | xargs -n1 -I @ echo -n '\"@\" '\r\n\"line1\" \"line2\" \"line3\" \"line4\" \"line5\" \"line6\" \"line7\" \"line8\" \"line9\" \"line10\" \"line11\" \"line12\" \"line13\" \"line14\" \"line15\" \"line16\" \"line17\" \"line18\" \"line19\" \"line20\" %<\/pre>\n<p>\u4e0a\u9762\u6d89\u53ca\u5230\u4e86xargs\u7684\u300c-n\u300d\u9009\u9879\u548c\u300c-I\u300d\u9009\u9879\uff0c\u5176\u9009\u9879\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n<blockquote><p><span style=\"color: #ff0000;\"><strong>-n max-args #\u6700\u591a\u8bfb\u53d6max-lines\u884c\u7ed9command\u547d\u4ee4\u6267\u884c\uff08\u9ed8\u8ba4\u662fecho\u547d\u4ee4\uff09<\/strong><\/span><br \/>\nUse at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>-I replace-str #\u53c2\u6570\u7684\u5360\u4f4d\u7b26\uff0c\u4e3a\u4f20\u5165\u7684\u53c2\u6570\u8bbe\u7f6e\u4e00\u4e2a\u522b\u540d\u65b9\u4fbf\u81ea\u5df1\u4f7f\u7528<\/strong><\/span><br \/>\nReplace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.<\/p>\n<p><span style=\"color: #ff0000;\">-L max-lines #\u4ece\u975e\u7a7a\u8f93\u5165\u4e2d\u8bfb\u53d6\u6700\u591amax-lines\u884c\u7ed9command\u547d\u4ee4\u6267\u884c\uff08\u9ed8\u8ba4\u662fecho\u547d\u4ee4\uff09<\/span><br \/>\nUse at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.<\/p>\n<p><span style=\"color: #0000ff;\"><strong>-P max-procs #\u5e76\u884c\u6a21\u5f0f<\/strong><\/span><br \/>\nRun up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.<\/p><\/blockquote>\n<pre class=\"lang:default decode:true \"># Parallel sleep\r\n$ time echo {1..5} | xargs -n 1 -P 5 sleep\r\n\r\nreal 0m5.013s\r\nuser 0m0.003s\r\nsys 0m0.014s\r\n\r\n# Sequential sleep\r\n$ time echo {1..5} | xargs -n 1 sleep\r\n\r\nreal 0m15.022s\r\nuser 0m0.004s\r\nsys 0m0.015s<\/pre>\n<p>\u901a\u8fc7\u4e00\u4e9b\u4f8b\u5b50\u6765\u8bf4\u660e\u5404\u4e2a\u9009\u9879\u7684\u4f5c\u7528\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$ cat test.txt | xargs -n2 # \u548c$(cat test.txt | xargs -L 2)\u547d\u4ee4\u7b49\u4ef7\r\nline1 line2\r\nline3 line4\r\n...\r\nline19 line20\r\n\r\n$ git log --format=\"%H %P\" | xargs -L 1 git diff<\/pre>\n<h5 id=\"Linux\u4e0bxargs\u547d\u4ee4\u7684\u7528\u6cd5\u5c0f\u7ed3-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a class=\"external-link\" href=\"http:\/\/offbytwo.com\/2011\/06\/26\/things-you-didnt-know-about-xargs.html\" rel=\"nofollow\">http:\/\/offbytwo.com\/2011\/06\/26\/things-you-didnt-know-about-xargs.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/stackoverflow.com\/questions\/6958689\/xargs-with-multiple-commands-as-argument\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/6958689\/xargs-with-multiple-commands-as-argument<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/stackoverflow.com\/questions\/17196230\/bash-print-each-input-string-in-a-new-line\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/17196230\/bash-print-each-input-string-in-a-new-line<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/unix.stackexchange.com\/questions\/89130\/format-output-of-xargs\" rel=\"nofollow\">https:\/\/unix.stackexchange.com\/questions\/89130\/format-output-of-xargs<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4e0b\u5348\u5728\u5904\u7406\u4e00\u4e9b\u811a\u672c\u95ee\u9898\u7684\u65f6\u5019\u7528\u5230\u4e86xargs\u547d\u4ee4\u7684\u4e00\u4e9b\u4e0d\u90a3\u4e48\u5e38\u7528\u7684\u7528\u6cd5\uff0c\u5728\u89e3\u51b3\u4e86\u5b9e\u9645\u95ee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[30,90],"class_list":["post-3611","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-linux","tag-xargs"],"views":3522,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3611"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3611\/revisions"}],"predecessor-version":[{"id":3612,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3611\/revisions\/3612"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}