On 8/7/07, Chas Owens <[EMAIL PROTECTED]> wrote:
>
> On 8/7/07, Mihir Kamdar <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I am trying to count the number of lines in my output file processed by
> a
> > perl code.
> >
> > I am not getting the syntax correct. I am doing something like below:-
> >
> > my $count1 = `wc -l < $OUT_FILE` ;
> > print $LOG_FILE "Count of cdrs after removing duplicates = $count1" ;
> >
> > I am getting the following error:
> > [EMAIL PROTECTED] files]$ sh: -c: line 0: syntax error near unexpected
> token
> > `('
> > sh: -c: line 0: `wc -l < GLOB(0x88f76d8)'
> > sh: -c: line 0: syntax error near unexpected token `('
> > sh: -c: line 0: `wc -l < GLOB(0x88f76d8)'
> >
> >
> > Plz advice,
> >
> > Thanks,
> > Mihir
> >
>
> That would be because $OUT_FILE is a file handle, not the file's name.
> You should also not be using the qx// operator (aka backticks) to do
> this, Perl can handle it just fine:
>
> sub count_words {
>     my $file = shift;
>     open my $fh, "<", $file
>         or die "could not open $file:$!";
>     my $lines = 0;
>     $lines++ while <$fh>;
>     return $lines;
> }
>
Following is my code:-

#!/usr/bin/perl

my $child_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/child'
;
my $parent_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files'
;
my $write_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/output'
;
my %times ;
my $continue  = 1;
my $count1 ;

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

while ($continue) {
opendir my $dh, $child_path or die $!;
        while (my $file = readdir $dh) {
                my $fname = "$child_path/$file";
                next unless -f $fname;
                        unless (exists $times{$file}){
                                my $line;
                                my %hash;
                                opendir my $dh1,$parent_path or die $!;
                                open (my $IN_FILE,"<","$parent_path/$file")
or die $!." Parent file not found for $parent_path/$file";

                                        my $count ;
                                        open (my
$LOG_FILE,">>","$write_path/logfile2") ;
                                        for ($count=0; <$IN_FILE>; $count++)
{ }
                                        print $LOG_FILE "Count of cdrs
before removing duplicates = $count" ;

                                        while ($line=readline($IN_FILE))
                                        {
                                        my @cdr=split (/,/, $line) ;
                                        $hash{"@cdr[2,3,6,7]"}=$line;  #Add
some more cdr key fields if u want.
                                        }
                                        close $IN_FILE ;
                        closedir $dh1 ;
                        print $LOG_FILE "Count of cdrs before removing
duplicates = $count" ;
                open (my $OUT_FILE,"+>","$write_path/$file.out") or die $!;

                while (my($key, $value) = each %hash)
                        {
                        print $OUT_FILE $value;
                        }
                        my $lines = 0 ;
                        $lines++ while<$OUT_FILE> ;

                        print $LOG_FILE "Count of cdrs after removing
duplicates = $lines" ;
                        close $OUT_FILE;
                        close $LOG_FILE ;
                }
                unlink("$parent_path/$file") ;
                unlink("$child_path/$file") ;
        }
closedir $dh;
sleep(2) ;
}


At last I am trying to get the count of lines of my OUT_FILE.  It is giving
me the result as 0, which is not true.
Where am i doing a mistake?

Thanks,
Mihir

Reply via email to