{"id":35,"date":"2004-05-01T00:18:57","date_gmt":"2004-04-30T22:18:57","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/05\/01\/perl-one-liners\/"},"modified":"2007-05-09T23:33:48","modified_gmt":"2007-05-09T21:33:48","slug":"perl-one-liners","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/05\/perl-one-liners\/","title":{"rendered":"Perl one-liners"},"content":{"rendered":"<p>Collection of Perl one liners. The Camel ownz j00 all :)<br \/>\n<!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-perl line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-perl\"># run contents of &quot;my_file&quot; as a program\r\n$ perl my_file\r\n\r\n# run debugger &quot;stand-alone&quot;\r\n$ perl -d -e 42\r\n\r\n# run program, but with warnings\r\n$ perl -w my_file\r\n\r\n# run program under debugger\r\n$ perl -d my_file\r\n\r\n# just check syntax, with warnings\r\n$ perl -wc my_file\r\n\r\n# useful at end of &quot;find foo -print&quot;\r\n$ perl -nle unlink\r\n\r\n# simplest one-liner program\r\n$ perl -e &#039;print &quot;hello world! &quot;&#039;\r\n\r\n# add first and penultimate columns\r\n$ perl -lane &#039;print $F[0] + $F[-2]&#039;\r\n\r\n# just lines 15 to 17\r\n$ perl -ne &#039;print if 15 .. 17&#039; *.pod\r\n\r\n# in-place edit of *.c files changing all foo to bar\r\n$ perl -p -i.bak -e &#039;s\/\\bfoo&amp;\\b\/bar\/g&#039; *.c\r\n\r\n# command-line that prints the first 50 lines (cheaply)\r\n$ perl -pe &#039;exit if $. &gt; 50&#039; f1 f2 f3 ...\r\n\r\n# delete first 10 lines\r\n$ perl -i.old -ne &#039;print unless 1 .. 10&#039; foo.txt\r\n\r\n# change all the isolated oldvar occurrences to newvar\r\n$ perl -i.old -pe &#039;s{\\boldvar\\b}{newvar}g&#039; *.[chy]\r\n\r\n# command-line that reverses the whole file by lines\r\n$ perl -e &#039;print reverse &lt;&gt;&#039; file1 file2 file3 ....\r\n\r\n# find palindromes\r\n$ perl -lne &#039;print if $_ eq reverse&#039; \/usr\/dict\/words\r\n\r\n# command-line that reverse all the bytes in a file\r\n$ perl -0777e &#039;print scalar reverse &lt;&gt;&#039; f1 f2 f3 ...\r\n\r\n# command-line that reverses the whole file by paragraphs\r\n$ perl -00 -e &#039;print reverse &lt;&gt;&#039; file1 file2 file3 ....\r\n\r\n# increment all numbers found in these files\r\n$ perl i.tiny -pe &#039;s\/(d+)\/ 1 + $1 \/ge&#039; file1 file2 ....\r\n\r\n# command-line that shows each line with its characters backwards\r\n$ perl -nle &#039;print scalar reverse $_&#039; file1 file2 file3 ....\r\n\r\n# delete all but lines beween START and END\r\n$ perl -i.old -ne &#039;print unless \/^START$\/ .. \/^END$\/&#039; foo.txt\r\n\r\n# binary edit (careful!)\r\n$ perl -i.bak -pe &#039;s\/Mozilla\/Slopoke\/g&#039; \/usr\/local\/bin\/netscape\r\n\r\n# look for dup words\r\n$ perl -0777 -ne &#039;print &quot;$.: doubled $_&amp;\\n&quot; while \/\\b(&amp;\\w+)\\b\\s+\\b\\1\\b\/gi&#039; \r\n\r\n# command-line that prints the last 50 lines (expensively)\r\n$ perl -e &#039;lines = &lt;&gt;; print @@lines[ $#lines .. $#lines-50&#039; f1 f2 f3 ...<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Collection of Perl one liners<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,3,9],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","tag-code","tag-linux","tag-perl"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}