Walt Mankowski on 17 Nov 2003 13:07:02 -0500


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

Re: [PLUG] search and replace multiple files?


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: pgpAJCneJCCrc.pgp
Description: PGP signature