John Fiore on 2 Dec 2003 16:10:03 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] search and replace multiple files?


This post was from a while ago.  I saved it because I
thought that it might come in handy.

Today was the first day that I tried using it, but I'm
getting an error.

If I create a simple file called "file1", which just
contains the text "foo" in certain places, and try
running the command as you've mentioned:

      perl -pie 's/foo/bar/' file1

I get the error:
     Can't open perl script "s/foo/bar/": No such file
or directory

I'm using bash, but the same thing occurs if I'm using
tcsh.  I'm probably doing something stupid.  Any
ideas?

Thanks.

John

--- Walt Mankowski <waltman@pobox.com> wrote:
> On Mon, Nov 17, 2003 at 12:09:54PM -0500, gabriel
> rosenkoetter wrote:
> > On Mon, Nov 17, 2003 at 08:59:32AM -0800, Marc
> Zucchelli wrote:
> > > before i resort to writing a quick perl script,
> does
> > > anyone know if this is possible via command
> line?  I
> > > have close to 100 files that i need to search
> and
> > > replace.
> > 
> > ed(1) in a for (syntax dependent on your shell)
> loop should do this.
> > 
> > Some thing like this:
> > 
> > for i in * ; do printf 's/foo/bar/\nwq\n' | ed $i
> ; done 
> 
> This sort of stuff is quite simple in Perl, too. 
> Run
> 
>   perldoc perlrun
> 
> and take a look at the -i switch.  The above command
> would be written
> as:
> 
>   perl -pie 's/foo/bar/' file1 file2 ...
> 
> It's less code than the equivalent shell script,
> *and* you get to say
> -pie.  Isn't perl great? :)
> 
> Perlrun has examples of how to make backups of the
> files you change.
> 
> Walt
> 

> ATTACHMENT part 2 application/pgp-signature 


___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug