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

I want to read a .gz file. Itried this code but its showing an error.

Output
$VAR1 = [ '/root/prac/NSN_SGSN/generate_ASR_PSR_report.pl.gz ', '/root/prac/NSN_SGSN/generate_nsn_sgsn_report.pl.gz ', '/root/prac/NSN_SGSN/month_run.pl.gz ', '/root/prac/NSN_SGSN/scope.pl.gz ', '/root/prac/NSN_SGSN/size.pl.gz ', '/root/prac/NSN_SGSN/System_Health_Details.pl.gz ', '/root/prac/NSN_SGSN/test.pl.gz ' ]; No such file or directory at gun.pl line 17.
Code
#!/usr/bin/perl use warnings; use strict; use Archive::Tar; use Data::Dumper; my $cdr_dir="/root/prac/NSN_SGSN"; my @cdr_list=`ls $cdr_dir/*tar.gz`; print Dumper \@cdr_list; foreach my $file (@cdr_list) { chomp $file; open my $fh, q{<}, qq{tar xzOF $file | } or die $!; while ( my $line = <$fh> ) { print $line; } close $fh; }

Replies are listed 'Best First'.
Re: Reading zipped files (.gz)
by Corion (Patriarch) on Nov 18, 2015 at 07:45 UTC

    Your filenames end with newlines. You likely do not want that.

    Also, the following code makes no sense:

    open my $fh, q{<}, qq{tar xzOF $file | } or die $!;

    Either use the two-argument version of open, with an appended pipe, or use the three argument version, with -|. Mixing two-argument and three-argument syntax makes no sense. Perl tries to open a file that has a name of "tar xzOF ...", and such a file presumably does not exist.

      atcroft Suggested that line in CB. I want to read the gz files line by line. At first I tried that using system command. But its taking lot of CPU & memory. Before starting that unzipping the memory is 77.63%. I tested that on 100MB data. After unzipping its showing 78.52%. Even the process exists the memory is not becoming free. So, I decided to read the unzip file directly using perl modules.

        What does your reply have to do with my remarks?

        I pointed out where problems in your program lie, which lead to you not being able to read the files. Have you addressed the immediate problems I pointed out?

        It is bad style to use `ls ...`. I prefer to use File::Glob::bsd_glob to expand wildcards. This also eliminates the need to remove whitespace at the end of filenames.

Re: Reading zipped files (.gz)
by vinoth.ree (Monsignor) on Nov 18, 2015 at 07:59 UTC

    Try the following code.

    #!/usr/bin/perl use warnings; use strict; use Archive::Tar; use Data::Dumper; my $cdr_dir="/root/prac/NSN_SGSN"; my @cdr_list=`ls $cdr_dir/*tar.gz`; print Dumper \@cdr_list; foreach my $file (@cdr_list) { chomp $file; my $pipecmd = "tar -xvf $file"; #open my $fh, q{<}, qq{tar xzOF $file | } or die $!; open(my $PIPEIN, '-|', $pipecmd) or die "Opening pipe [$pipecm +d]: $!\n"; while ( my $line = <$PIPEIN> ) { print $line; } close $PIPEIN; }
    Update:

    Try whatever the option you want with the tar command, I used -xvf for sample tesing.

    You have included Archive::Tar why do not you use that module to read the tar file content ?


    All is well. I learn by answering your questions...

      I tried that module but cant reached up to that extend. If you have any sample code on to read .gz files (Not tar.gz) please post it here. Its very helpful for me

Re: Reading zipped files (.gz)
by GotToBTru (Prior) on Nov 18, 2015 at 15:14 UTC

    You include Archive::Tar in your code and your @cdr_list array is meant to be populated with a list of (gzipped) tars, but your dump of the variable doesn't match your command since none of those files is a tar. So, assuming the mention of tar is an artifact and you really just need to deal with zipped files:

    use IO::Zlib; my $fh = new IO::Zlib; $fh->open('zipfile.gz','rb'); while(<$fh>) { print } $fh->close

    If/when you decide to deal with gzipped tars, Archive::Tar can open gzipped files automatically without needing to explicitly unzip them.

    chomp @cdr_list; foreach my $tarzip (@cdr_list) { my $tar = Archive::Tar->new($tarzip); foreach my $file ($tar->list_files) { print $tar->get_content($file) } }
    Dum Spiro Spero