Charlotte Hee wrote:

Hello All,

Hello,

I have a set of gzipped files and I would like to unzip and read the
contents using perl v5.8 and gzcat. The location of gzcat is '/usr/bin/gzcat' and the filenames are in the format:
   batch.shares.2007-07-24.gz
where the date changes but the rest of the name is the same. All the files in the directory are rw-rw-rw so I should be able to read them. I use opendir and readdir to read in all the filenames and then open to read the contents. But what I tried failed to read the file contents.
The following is not the full program, just the section that tries
to open and read the files:

$GZCAT = '/usr/bin/gzcat';
$file_location = '/nfs/workdir/batch/';

opendir(BAT, "$file_location") || die "Cannot open $file_location\n";
while ( $input = readdir BAT ){
  next if $input =~ /^\.\.?$/;   # skip the dot files
  open(IN,"$GZCAT $file_location/$input") || die "Cannot read $input\n";
     while ( my $line = <IN> ){
        # do something
     }
  }
}

opendir and readdir work and get the filenames but open(IN,"$GZCAT $file_location/$input") complains
that it can't read the file. I printed out the line
"$GZCAT $file_location/$input" and then tried it
from the command line and it works so I'm at a loss
why it doesn't work from my script.

Any advice is greatly appreciated.

gzcat sends its output to STDOUT so you have to pipe that output to your 
program:

opendir BAT, "$file_location" or die "Cannot open $file_location\n $!";
while ( my $input = readdir BAT ) {
  next if $input =~ /^\.\.?$/;   # skip the dot files
  open IN, '-|', "$GZCAT $file_location/$input" or die "Cannot read $input\n 
$!";
     while ( my $line = <IN> ) {
        # do something
     }
  close IN or warn $! ? "Error closing $GZCAT pipe: $!"
                      : "Exit status $? from $GZCAT";
  }
}



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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to