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

Hi all, The following code works fine when dealing with a single file, but having troubles with mulitple files. Essentially, takes each file found, replaces the first few columns of this file with a set of columns form another file called replace. The main i think is the way i am printing to an output file, it might be better to overwrite the file in question. Any suggestions?
@files = File::Find::Rule->file ->name('complex.*') ->in ("/home"); $new = 'replace'; open (REPLACE, "<$new"); while (<REPLACE>) { push @new, $_ =~ m/^(\S+\s+\S+\s+\S+)/; } close (REPLACE); foreach $file (@files) { open (FH, "<$file"); open (OUT, ">output"); while (<FH>) { $_ =~ s/^\S+\s+\S+\s+\S+/$new[$i++] /; print OUT $_; $count++; rename "NEWNAMES", "NEWNAMES$count"; } close(FH); }

20040928 Edit by Steve_p: Changed title from 'File::Find'

Replies are listed 'Best First'.
Re: Replacing text in multiple files using File::Find
by si_lence (Deacon) on Sep 28, 2004 at 13:38 UTC
    I don't think your problem lies with File::Find.
    In the while(<FH>) loop you rename your (open) outfile for each line you're reading.

    My guess would be you want something like this:
    use strict; use warnings; use File::Find::Rule; my @files = File::Find::Rule->file ->name('complex.*') ->in ("/home"); my $new = 'replace'; open (REPLACE, "< $new") || die "could not open $new: $!"; ; my @new; while (<REPLACE>) { push @new, $_ =~ m/^(\S+\s+\S+\s+\S+)/; } close (REPLACE); my $count=0; foreach my $file (@files) { open (FH, "< $file") || die "could not open $file for input: $!";; open (OUT, "> output") || die "could not open 'output' for output: + $!";; my $i=0; while (<FH>) { $_ =~ s/^\S+\s+\S+\s+\S+/$new[$i++] /; print OUT $_; } close(FH) || die "could not close FH: $!"; ; close(OUT)|| die "could not close OUT: $!"; $count++; rename "output", "output$count"; }

    Also make it a habit to run under strict and warnings and test your open calls.
    (I spent too much time myself wondering what happened when "use warnings" would
    have told me right away ;-)
    si_lence
Re: Replacing text in multiple files using File::Find
by fjaenale (Acolyte) on Sep 28, 2004 at 14:44 UTC
    Hello:

    On the line where you defined the handle for the output file open (OUT, ">output"); you have to use >> intead of > because you are creating a new output file each time you have to iterate the foreach.

    Resuming the code should be: open (OUT, ">>output");.

    Francisco Jaen

Re: Replacing text in multiple files using File::Find
by Anonymous Monk on Sep 28, 2004 at 12:48 UTC
    sorry, NEWNAMES should read output