<story><title>Useful Perl one-liners (2013)</title><url>http://www.catonmat.net/download/perl1line.txt</url></story><parent_chain><item><author>jxy</author><text>What is the advantage of this over sed?</text></item><item><author>shanemhansen</author><text>My favorite perl one liner is a globbed search and replace. It even has it&amp;#x27;s own website: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;perlpie.com&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;perlpie.com&amp;#x2F;&lt;/a&gt;&lt;p&gt;&lt;pre&gt;&lt;code&gt;     perl -p -i -e &amp;#x27;s&amp;#x2F;foo&amp;#x2F;bar&amp;#x2F;gi&amp;#x27; .&amp;#x2F;*.txt&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>paulmd</author><text>They&amp;#x27;re functionally interchangeable for simple usage (minus some perl-specific stuff like PCRE).&lt;p&gt;As I note below, escape behavior is a little different because sed wants you to escape +&amp;#x27;s to have the normal regex semantics (&amp;quot;one or more matches&amp;quot;).  And I actually think Perl is correct here and you should only need to escape those characters if you want &lt;i&gt;literal matches&lt;/i&gt;, but I have a weird environment (Cygwin) and it&amp;#x27;s possible the sed build there is a little messed up.&lt;p&gt;The major difference for me is that Perl can match across multiple lines using the -0777 flag.  I&amp;#x27;ve been doing a lot of regex-based mass manipulation of source code lately and most people write functions across multiple lines.  You can&amp;#x27;t do that with sed without multi-line appending and it gets really ugly really fast.  Sed is pretty much just single line matches only.&lt;p&gt;For example, I had 100-odd classes with getters for certain values but not setters.  So I did:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  grep -rle &amp;quot;getAddTime&amp;quot; | while read line; do if ! grep -q &amp;quot;setAddTime&amp;quot; $line ; then echo $line; perl -i -0777 -pe &amp;#x27;s&amp;#x2F;public\s+Date\s+getAddTime\s*\(\s*\)\s*\{[\s\w=;]+\}&amp;#x2F;$&amp;amp;\n\n  public void setAddTime(Date addTime) { this.addTime = addTime; }&amp;#x2F;&amp;#x27; $line; fi done
&lt;/code&gt;&lt;/pre&gt;
translation: look for files that contain getAddTime, if they do not contain setAddTime then find the string &amp;quot;public Date getAddTime() {...} and append the setter after that&amp;quot;.  There are a few edge cases you could hit there but it was close enough to work on my codebase.&lt;p&gt;I wish Perl would do an inplace edit of a file without creating a backup, though.  I am under source control so there&amp;#x27;s no harm in just operating right on the files.  It&amp;#x27;s not the end of the world to follow up with a rm -r *.bak I guess, but it&amp;#x27;s annoying.  At least they&amp;#x27;re in my git-ignore which helps a little.</text></comment>
<story><title>Useful Perl one-liners (2013)</title><url>http://www.catonmat.net/download/perl1line.txt</url></story><parent_chain><item><author>jxy</author><text>What is the advantage of this over sed?</text></item><item><author>shanemhansen</author><text>My favorite perl one liner is a globbed search and replace. It even has it&amp;#x27;s own website: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;perlpie.com&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;perlpie.com&amp;#x2F;&lt;/a&gt;&lt;p&gt;&lt;pre&gt;&lt;code&gt;     perl -p -i -e &amp;#x27;s&amp;#x2F;foo&amp;#x2F;bar&amp;#x2F;gi&amp;#x27; .&amp;#x2F;*.txt&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>dozzie</author><text>&amp;gt; What is the advantage of [perl -pi -e &amp;#x27;s&amp;#x2F;&amp;#x2F;&amp;#x2F;&amp;#x27;] over sed?&lt;p&gt;PCRE and &amp;#x2F;e flag.</text></comment>