{"id":134,"date":"2014-06-25T15:05:28","date_gmt":"2014-06-25T15:05:28","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=134"},"modified":"2014-06-25T15:05:28","modified_gmt":"2014-06-25T15:05:28","slug":"%e8%bd%acperl-%e5%91%bd%e4%bb%a4%e8%a1%8c%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/134.html","title":{"rendered":"Perl\u7684\u547d\u4ee4\u884c\u7f16\u7a0b"},"content":{"rendered":"<p>\u5728Linux\u7cfb\u7edf\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f7f\u7528Perl\u6709\u65f6\u5019\u53ef\u4ee5\u8d77\u5230\u5f88\u597d\u7684\u6548\u679c(\u7279\u522b\u662f\u5728\u7528Bash\u505a\u8d77\u6765\u6bd4\u8f83\u590d\u6742\uff0cPython\u7684\u7248\u672c\u53c8\u592a\u9648\u65e7\u7684\u65f6\u5019)\uff0c\u4e0b\u9762\u8bb0\u5f55\u4e00\u4e9b\u4ece\u7f51\u4e0a\u627e\u5230\u7684Perl\u547d\u4ee4\u884c\u7f16\u7a0b\u7684\u6559\u7a0b\uff1a<\/p>\n<hr \/>\n<p>-e \u540e\u9762\u7d27\u8ddf\u7740\u5f15\u53f7\u91cc\u9762\u7684\u5b57\u7b26\u4e32\u662f\u8981\u6267\u884c\u7684\u547d\u4ee4\uff1a<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ perl -e 'print \"hello world \\n\"'\nhello world<\/pre>\n<p>\u5982\u679c\u662f\u591a\u4e2a\u547d\u4ee4\u5c31\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a-e\u9009\u9879\uff0c\u8fd9\u91cc\u662f\u4e0d\u662f\u60f3\u5230\u4e86sed\u5462\uff1f\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\u4e2d\u95f4\u662f\u7528\u7684&#8221;;&#8221;\u5206\u9694\u3002<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ perl -e 'print \"hello world \\n\";' -e 'print \"my first perl command line script \\n\"'\nhello world\nmy first perl command line script<\/pre>\n<p>-M \u5bfc\u5165\u8981\u4f7f\u7528\u7684\u6a21\u5757:<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ perl -MLWP::Simple -e 'print head \"http:\/\/ixyzero.com\",\"\\n\"'\ntext\/html; charset=UTF-8nginx<\/pre>\n<p>\u53e6\u5916 -M \u53ef\u4ee5\u5224\u65ad\u67d0\u4e2a\u6a21\u5757\u662f\u4e0d\u662f\u5df2\u7ecf\u5b89\u88c5\u4e86\u3002<\/p>\n<p>-m \u7684\u529f\u80fd\u548c -M \u7c7b\u4f3c\uff0c\u533a\u522b\u5728\u4e8e -M \u6a21\u5757\u540d\u7b49\u540c\u4e8e use \u6a21\u5757\u540d\uff0c\u8fd9\u6837\u4f1a\u628a\u4e00\u4e9b\u9ed8\u8ba4\u7684\u51fd\u6570\u6216\u8005\u5176\u5b83\u4e1c\u897f\u5f15\u8fdb\u8fc7\u6765\uff0c-m \u6a21\u5757\u540d \u5219\u4f1a\u5173\u95ed\u8fd9\u4e9b\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u53ef\u4ee5\u53ea\u5f15\u5165\u4e00\u4e9b\u4f60\u8981\u4f7f\u7528\u7684\u51fd\u6570\uff1b\u4f8b\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a\u6362\u6210-m\u7684\u8bdd\u5c31\u4e0d\u4f1a\u6709\u4efb\u4f55\u8f93\u51fa\uff1b-m \u548c -M \u901a\u8fc7 = \u6765\u5f15\u5165\u67d0\u4e2a\u6a21\u5757\u7684\u7279\u522b\u51fd\u6570\u3002<\/p>\n<pre class=\"lang:default decode:true\">$ perl -MCGI='header,start_html' -e 'print header, start_html'\nContent-Type: text\/html; charset=ISO-8859-1\n\n&lt;!DOCTYPE html\n        PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\"\n         \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" lang=\"en-US\" xml:lang=\"en-US\"&gt;\n&lt;head&gt;\n&lt;title&gt;Untitled Document&lt;\/title&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\" \/&gt;\n&lt;\/head&gt;\n&lt;body&gt;<\/pre>\n<p>-w \u7b49\u540c\u4e8euse warnings<\/p>\n<p>-n -p \u90fd\u4f1a\u4f7f\u7528 &lt;&gt; \u5c06\u6240\u6709 @ARGV \u53c2\u6570\u5f53\u4f5c\u6587\u4ef6\u6765\u9010\u884c\u8fd0\u884c(\u6709\u5faa\u73af\u7684\u610f\u601d\uff0c\u7ecf\u5e38\u548c\u5176\u5b83\u53c2\u6570\u4e00\u8d77\u5904\u7406\u6587\u4ef6)\uff0c\u4f1a\u5c06\u8bfb\u5165\u7684\u5185\u5bb9\u9690\u5f0f\u7684\u9010\u4e00\u6309\u884c\u6765\u904d\u5386\u6587\u4ef6\u3002<span style=\"color: #ff0000;\"><strong>\u6bcf\u4e00\u884c\u5c06\u7f3a\u7701\u4fdd\u5b58\u5728 $_ \u4f46 -p \u4f1a\u5c06\u5185\u5bb9&#8221;\u91cd\u590d&#8221;\u6253\u5370\u51fa\u6765\uff0c\u800c -n \u66f4\u503e\u5411\u4e0e\u6253\u5370\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u884c(\u8fd9\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e9b\u6709\u7528\u7684\u53d8\u91cf\u6bd4\u5982 $. \u8868\u793a\u5f53\u524d\u884c\u7684\u884c\u53f7<\/strong><\/span>)\uff1a<\/p>\n<pre class=\"lang:default decode:true  \">hi@ixyzero.com:~$ cat file.txt\nA 1\nB 2\nC 3\nhi@ixyzero.com:~$ perl -p -e 'print ' file.txt\nA 1\nA 1\nB 2\nB 2\nC 3\nC 3\nhi@ixyzero.com:~$ perl -n -e 'print ' file.txt\nA 1\nB 2\nC 3<\/pre>\n<p>\u770b\u5230\u8fd9\u91cc\u662f\u4e0d\u662f\u60f3\u5230\u4e86sed\u7684 -n \u7684\u7279\u6027\uff1a<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ sed -ne 'p' file.txt\nA 1\nB 2\nC 3\nhi@ixyzero.com:~$ sed -e 'p' file.txt\nA 1\nA 1\nB 2\nB 2\nC 3\nC 3<\/pre>\n<p>-i \u5c06\u4fee\u6539\u76f4\u63a5\u7684\u5199\u5165\u6587\u4ef6\uff0c\u8fd9\u4e2a\u548csed\u4e5f\u662f\u4e00\u6837\u7684\u54e6\uff1b<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ cat file.txt\nA 1\nB 2\nC 3\nhi@ixyzero.com:~$ perl -pi -e 's\/A\/a\/' file.txt\nhi@ixyzero.com:~$ cat file.txt\na 1\nB 2\nC 3<\/pre>\n<p>-a \u6253\u5f00\u81ea\u52a8\u5206\u79bb (split) \u6a21\u5f0f\u3002\u7a7a\u683c\u662f\u7f3a\u7701\u7684\u5206\u79bb\u53f7\uff0c\u8f93\u5165\u6839\u636e\u5206\u79bb\u53f7\u88ab\u5206\u79bb\u7136\u540e\u653e\u5165\u7f3a\u7701\u6570\u7ec4 @F \u3002<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ perl -na -e 'print $F[1],\"\\n\"' file.txt\n1\n2\n3<\/pre>\n<p>\u548cawk\u662f\u4e0d\u662f\u5f88\u50cf\uff1f\u8fd8\u6709\u66f4\u50cf\u7684\uff0c\u90a3\u5c31\u662f-F\uff0c\u548cawk\u4e00\u6837\u66f4\u6539\u5b57\u6bb5\u4e4b\u95f4\u7684\u5206\u5272\u7b26\uff0c\u9ed8\u8ba4\u662f\u7a7a\u683c\u548ctab\uff1b<\/p>\n<pre class=\"lang:default decode:true\">hi@ixyzero.com:~$ perl -F':' -alne 'print $F[0],\" \",$F[-1] if \/bash$\/' \/etc\/passwd\nroot \/bin\/bash\npostgres \/bin\/bash<\/pre>\n<p>\u770b\u5230\u4e0a\u9762\u7684\u90a3\u4e2a -l \u7684\u53c2\u6570\u6ca1\uff1f\u5b83\u7684\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u5c31\u662f chomp \u5b57\u6bb5\u548c\u5728\u6bcf\u4e2a\u8f93\u51fa\u7684\u540e\u9762\u52a0\u4e0a&#8221;\\n&#8221;\u8f93\u51fa\u6362\u884c\uff0c\u662f\u4e0d\u662f\u633a\u6709\u7528\u7684\u5462\uff1f<\/p>\n<p>\u6709\u6ca1\u6709\u5176\u5b83\u548cawk\u6709\u7c7b\u4f3c\u7684\u5730\u65b9\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0cBEGIN{},END{} \u662f\u4e0d\u662f\u4e5f\u5f88\u719f\u6089\u5462\uff1f\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7b80\u5355\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$ perl -pi -e's\/aaa\/bbb\/' filename  #\u76f4\u63a5\u4fee\u6539\u5f53\u524d\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u4e0d\u751f\u6210\u4e2d\u95f4\u6587\u4ef6\uff0c\u4e00\u5b9a\u8981\u4f7f\u7528 -i \u5f00\u5173\n$ perl -ne 'print if \/^aaaaa\/' filename #\u50cfgrep\u4e00\u6837\u8fc7\u6ee4\u6587\u4ef6\u4e2d\u9700\u8981\u7684\u5185\u5bb9\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86 -n \u6240\u4ee5\u662f\u4e00\u6b21\u505a\u4e00\u884c\u7684\u64cd\u4f5c\uff0c\u76f4\u5230\u6574\u4e2a\u6587\u4ef6\u8bfb\u5b8c\uff1b\u53e6\u5916\uff1b\u5728\u4f7f\u7528\u7ba1\u9053\u65f6 -n \u4e5f\u4f1a\u4e00\u6837\u4f9d\u6b21\u904d\u5386\u7ba1\u9053\u9001\u8fc7\u6765\u7684\u5185\u5bb9\n$ perl -n -e 'print \"$. \u2013 $_\"' filename #\u8fd9\u91cc\u6ca1\u7528 -ne \u800c\u662f\u5199\u6210\u4e86 -n -e \uff0c\u4f46\u6548\u679c\u4e00\u6837\uff1a\u7ed9\u5f53\u524d\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u6dfb\u52a0\u884c\u53f7\u5e76\u6253\u5370\u3002\u6ce8\uff1a$. \u8868\u793a\u5f53\u524d\u884c\u53f7\n$ perl -pe '$_ = \"$. $_\"' filename  #\u8fd9\u4e2a\u5176\u5b9e\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u53ea\u662f\u4f7f\u7528 -p \u66ff\u6362\u4e86 -n \uff0c\u8fd9\u4e2a\u6709\u4e2a\u4ec0\u4e48\u597d\u5904\u5462\uff1f\u522b\u7684\u5730\u65b9\u90fd\u4e00\u6837\uff0c\u4f46 -p \u6309\u884c\u904d\u5386\u5b8c\u6587\u4ef6\u540e\uff0c\u4f1a\u5c06 $_ \u6253\u5370\u51fa\u6765<\/pre>\n<p>\u5927\u5bb6\u8fd8\u8bb0\u5f97awk\u5206\u5272\u57df(awk &#8216;{i = NF \u2013 1; print $1 + $i}&#8217;)\u4e48\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770bperl\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$ perl -lane 'print $F[0] + $F[-2]' #\u8fd9\u91cc\u7684\u6280\u5de7\u5728\u4e8e -a \uff0c\u4f7f\u7528 -a \u5c06\u6570\u636e\u5206\u5272\u6210@F\u7684\u6570\u7ec4\n$ perl -ne 'print if \/^START$\/ .. \/^END$\/'  #\u6253\u5370\u6b63\u5219\u4e2d\u4ece START \u5230 END \u7684\u5730\u65b9\n$ perl -ne 'print if $. &gt;= 15; exit if $. &gt;= 17;'   #\u6709\u6548\u5730\u6253\u5370\u6570\u5b57\u8303\u56f4\u4e2d\u7684\u884c\n$ perl -p -i.bak -e 's\/\\bfoo\\b\/bar\/g' *.c   #\u539f\u5730\u4fee\u6539 -i \u5f00\u5173\u7684\u795e\u5947\u4e4b\u5904\u5728\u4e8e\u5b83\u5bf9 @ARGV \u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u90fd\u7528\u8be5\u811a\u672c\u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8f93\u51fa\u6240\u4ea7\u751f\u7684\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u66ff\u6362\n$ perl -ne 'print scalar reverse $_' filename   #\u5bf9\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u53cd\u5411\u6392\u5e8f\uff0c\u6bd4\u5982\u6587\u4ef6\u4e2d\u6709abc\uff0c\u5c31\u4f1a\u6253\u5370\u51facba<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.perl.com\/pub\/2004\/08\/09\/commandline.html\">http:\/\/www.perl.com\/pub\/2004\/08\/09\/commandline.html<\/a><\/li>\n<li><a href=\"http:\/\/www.chenqing.org\/2012\/05\/perl-command-line.html\">http:\/\/www.chenqing.org\/2012\/05\/perl-command-line.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u7cfb\u7edf\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f7f\u7528Perl\u6709\u65f6\u5019\u53ef\u4ee5\u8d77\u5230\u5f88\u597d\u7684\u6548\u679c(\u7279\u522b\u662f\u5728\u7528Bash\u505a\u8d77\u6765\u6bd4\u8f83\u590d\u6742\uff0cPytho [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[85],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-perl"],"views":2837,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/134","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}