http://www.perlmonks.org?node_id=1003607

najiz has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code to replace text in one file. How do I modify this code to replace text in multiple files with same extension.

#!/usr/bin/perl $file = "new.txt"; open (IN,$file) || die "could not open ".Sfile." for read"; @lines=<IN>; close IN; open (OUT,">", $file) || die "can not open file ".$file." for write"; foreach $line (@lines) { $line =~ s/Hello/goodby/ig; print OUT $line; } close OUT;
*********************************

Replies are listed 'Best First'.
Re: Replace strings in multiple files
by zentara (Archbishop) on Nov 13, 2012 at 13:18 UTC
    Something like this basic File::Find code should show you the right technique. Where I do the print, open and process the file.
    #!/usr/bin/perl use warnings; use strict; use File::Find; use File::Basename; my $dir = '.'; my @files; my @exts = qw(wbmp wmf jpeg jpe jpg ani bmp gif ico cur pcx png pnm pb +m pgm ppm ras tga targa xbm tiff tif xpm svg svgz); find( sub { my $file = $File::Find::name; my ($name, $path, $extension) = fileparse($file,'\..*'); substr $extension, 0, 1, ''; #remove leading . if( grep {$_ eq $extension} @exts ){ print "file - $file ext = $extension\n" ; } }, $dir ); #print "@files\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Replace strings in multiple files
by space_monk (Chaplain) on Nov 13, 2012 at 13:39 UTC
    Why not simply supply the filenames on the command line, and let the OS do filename expansion for you?

    e.g ./myprogram *.txt

    for my $file (@ARGV) { open (IN, $file); .... }
    A Monk aims to give answers to those who have none, and to learn from those who know more.

      Why not? Well, a few reasons, I suppose.

      First off, not all OS's do filename expansion. CMD.EXE doesn't, for example, at least not last time I tried.

      Second, maybe the filename is coming as input from an input file. Putting filenames as a command line parameter isn't always a good option. Maybe it's being run automatically through a web interface without any use of a shell.

      Third, maybe the filenames are remote files, files in another directory, or any number of other reasons.

      The answer to all of the above is glob (assuming you don't need to recurse subdirs).

        The answer to all of the above is glob

        No.

        Most shells do expand filenames, and for the special case Win32, there is a workaround available (see also Re^3: Perl Rename). This way, the command line interface for a program is consistent across platforms.

        If you pass the programs arguments to glob unconditionally, you have to quote the wildcards with every shell that expands filenames, but you must not quote them with shells that don't expand filenames.

        First off, not all OS's do filename expansion. CMD.EXE doesn't, for example, at least not last time I tried.

        CMD.EXE is not an operating system. It is a shell. Butt ugly, loaded with bug-compatibility back to DOS 1.0, and in desperate need of a clean rewrite, but still a shell, like its slightly uglier father command.com.

        Expanding filenames does not depend on the OS. Cygwin offers a bash running on top of Windows, and I would be very surprised if it did not expand filenames. Implementing a tiny and stupid shell that does not expand filenames on Unix is no problem, but people rarely want that. It is a good homework, to understand how a shell works. And it may be useful for a restricted shell.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Actually, forget my previous answer, what is wrong with
      perl -i -p -e 's/Hello/goodbye/ig' *.txt
      or similar??
      A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Replace strings in multiple files
by Anonymous Monk on Nov 13, 2012 at 13:51 UTC