Anthony Saffer wrote:
> 
> Hello Everyone,

Hello,

> Here is the final code for my replacement script. I am about to test it
> out but I think it will work. Thanks to all who helped!!
> 
> #!/usr/bin/perl -w
> 
> use File::Find;
> 
> my $FileCount = 0;
> my $LineCount = 0;
> 
> sub process_files {
>    my $FinalString = "";
>    my $ConvertedText = "";
>    my $TotalReplacements = 0;
> 
>    open MASTER_FILE, "/home/losttre/sorted.txt" or die ("Can't open file: 
>sorted.txt! Aborting!\n");
>    open TEMP_FILE, "> tempfile" or die("Can't open file: TEMPFILE! Aborting!\n");
>    open THIS_FILE, "< $_" or die("Can't open target file: $_! Aborting!\n");
> 
>    @MATCH_KEYS    = <MASTER_FILE>;
>    @MATCH_TARGETS = <THIS_FILE>;
> 
>    foreach $matchkey (@MATCH_KEYS) {
>       foreach $lineitem (@MATCH_TARGETS) {
>          if($lineitem =~ m/$matchkey/i){
>             $ConvertedText = lc($matchkey);
>             $FinalString = s/$matchkey/$ConvertedText/;
>             print TEMP_FILE "$FinalString\n";
>             $LineCount++;
>             $TotalReplacements++
>          }
>          else{
>             print TEMP_FILE "$lineitem\n";
>             $LineCount++;
>       }
>      }
>       close(TEMP_FILE);
>       close(THIS_FILE);
>      # unlink(THIS_FILE)
>       #rename(TEMP_FILE, THIS_FILE);
>       $FileCount++;
>    }
> }
> 
> print "Process starting...\n";
> print "Cycling through files. This might take some time.\n";
> find(\&process_files, @ARGV);
> print "DONE!\n";
> print "FILES PROCESSED: $FileCount\n";
> print "LINES PROCESSED: $LineCount\n";
> print "REPLACEMENTS: $TotalReplacements\n";
> print "Exiting...";


This may work better (or at least be faster):

#!/usr/bin/perl -w
use strict;
use File::Find;

my $FileCount = 0;
my $LineCount = 0;
my $TotalReplacements = 0;

open MASTER_FILE, '/home/losttre/sorted.txt' or die "Can't open file: sorted.txt: $!";
my @match_keys = map { chomp; [ qr/$_/i, lc ] } <MASTER_FILE>;
close MASTER_FILE;

sub process_files {
   return unless -f;  # process files only

   rename $_, 'tempfile' or die "Can't rename $_: $!";
   open THIS_FILE, "> $_" or die "Can't open file: $_: $!";
   open TEMP_FILE, 'tempfile' or die "Can't open file: tempfile: $!";

   while ( my $lineitem = <TEMP_FILE> ) {
      chomp $lineitem;
      for my $matchkey ( @match_keys ) {
         $TotalReplacements++ if $lineitem =~ s/$matchkey->[0]/$matchkey->[1]/;
      }
      print THIS_FILE "$lineitem\n";
      $LineCount++;
   }

   close TEMP_FILE;
   close THIS_FILE;
   unlink 'tempfile';
   $FileCount++;
}

print "Process starting...\n";
print "Cycling through files. This might take some time.\n";
find( \&process_files, @ARGV );
print "DONE!\n";
print "FILES PROCESSED: $FileCount\n";
print "LINES PROCESSED: $LineCount\n";
print "REPLACEMENTS: $TotalReplacements\n";
print "Exiting...";

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to