[Chicago-talk] replace in place

Greg Fast gdf at speakeasy.net
Fri Mar 11 12:55:50 PST 2005


On Fri, 11 Mar 2005 14:29:51 -0600, Andy Lester <andy at petdance.com> wrote:
> On Fri, Mar 11, 2005 at 02:20:20PM -0600, JT Smith (jt at plainblack.com) wrote:
> > I remember having a really great tutorial on doing things in one line from 
> > the command line in perl, but I can't remember how it all worked. Could 
> or
> 
>  perl -i -p -l -e'$_ = "prefix=/data/prereqs" if $_ eq "prefix=/usr/local"' Makefile
> 
> You can replace
> 
>     Makefile
> 
> with
> 
>     `find . -name Makefile`
> 
> to find all Makefiles in the tree if you like.

This is a good trick too:

  find . -name \*.java | xargs perl -ipe 's/public/private/'

especially when you need complex findage, your file count starts to
bump up against the command length limit of your shell (usually ~1024
characters).  And if some of your filenames might have spaces or other
unsafe chars in them, "find ... -print0 | xargs -0 perl ..." rules.

Oh, and since perl -pe was mentioned, I'm karmically obliged to give a
shout out for

  perl -MO=Deparse -i@@ -pe 's/foo/bar/' Makefile

--
Greg Fast
http://cken.chi.groogroo.com/~gdf/


More information about the Chicago-talk mailing list