Re: using wc -l in perl



John W. Krahn wrote:

I rewrote your code so that:
1) It opens the log file once at the start;
2) Adds more error checking;
3) Prints an accurate count of input and output lines;
4) Preserves the same order of the input lines on output;
so hopefully this will work better for you.


#!/usr/bin/perl
use warnings;
use strict;

my $parent_path = '/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files';

my $child_path = "$parent_path/child";
my $write_path = "$parent_path/output";

my $continue = 1;
$SIG{ INT } = $SIG{ TERM } = sub { $continue = 0 };

# open log file and autoflush it
open my $LOG_FILE, '>>', "$write_path/logfile2" or die "Cannot open '$write_path/logfile2' $!";
select( ( select( $LOG_FILE ), $| = 1 )[ 0 ] );

my %times;
while ( $continue ) {

opendir my $dh, $child_path or die "Cannot open '$child_path' $!";

for my $file ( grep -f "$child_path/$file", readdir $dh ) {

** Correction **

for my $file ( grep -f "$child_path/$_", readdir $dh ) {


unless ( exists $times{ $file } ) {

open my $IN_FILE, '<', "$parent_path/$file" or die "Cannot open '$parent_path/$file' $!";
my $count = 0;
my %hash;
while ( my $line = <$IN_FILE> ) {
$count++;
my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
$hash{ $key } = [ $., $line ];
}
close $IN_FILE;

print $LOG_FILE "Count of cdrs before removing duplicates = $count\n";

open my $OUT_FILE, '>', "$write_path/$file.out" or die "Cannot open '$write_path/$file.out' $!";

$count = 0;
for my $line ( map $hash{ $_ }[ 1 ], sort { $hash{ $a }[ 0 ] <=> $hash{ $b }[ 0 ] } keys %hash ) {
$count++;
print $OUT_FILE $line;
}
close $OUT_FILE;

print $LOG_FILE "Count of cdrs after removing duplicates = $count\n";
}

unlink "$parent_path/$file" or warn "Cannot unlink '$parent_path/$file' $!";
unlink "$child_path/$file" or warn "Cannot unlink '$child_path/$file' $!";
}

closedir $dh;
sleep 2;
}

close $LOG_FILE;

__END__


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: using wc -l in perl
    ... John W. Krahn wrote: ... It opens the log file once at the start; ... unlink "$parent_path/$file" or warn "Cannot unlink ...
    (perl.beginners)
  • Re: [linux-cifs-client] Re: unlink behavior when file is open by other process
    ... Both open and unlink man pages list plausible return codes but I ... opens file ... So this will work against a server that doesn't support rename by ... The create will fail. ...
    (Linux-Kernel)
  • Re: Importing *.log records
    ... own VBA code that opens each file, reads the data in line by line and ... Dim intFile As Integer ... I can manually delete the header records from each log file. ... Editing the Windows Registry can cause serious problems if not ...
    (microsoft.public.access.externaldata)
  • CreateFile fails with error 32 in Windows CE 5.0
    ... I am writing to a log file in one thread of my program. ... This is the call which opens the handle to the file for writing: ... I've tested very similar code in Windows XP and never had any problems ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Writing a trace log file - permission problem
    ... impersonating - just write to the file. ... the System account or as a user with admin rights. ... It writes a log file - a simple text file. ... entry it opens the file, writes the data, then closes the file. ...
    (microsoft.public.win32.programmer.kernel)