in reply to Searching within compressed files

 open(IN,"zcat $myfile|") or die "Cannot open file: $myfile\n"; # This pipes the contents of the file to IN file handle

would something like this work? I am assuming you are on a system where zcat works on .Z and .gz files. For other compressions use corresponding utility and pipe it to Perl.

cheers

SK Update: Here is a snippet. Sorry I couldn't get the file reading and matching in one line :(

#!/usr/local/bin/perl -w my $myfile = "test.gz"; open(IN,"zcat $myfile|") or die "Cannot open file: $myfile\n"; while (<IN>) { print ($_) if /mymatch/; }

Replies are listed 'Best First'.
Re^2: Searching within compressed files
by revdiablo (Prior) on Apr 15, 2005 at 18:22 UTC

    Since I'm a big fan of both lexical filehandles, and the safer forms of piping open, here's a version that uses both of those:

    use strict; use warnings; open my $infh, "-|", zcat => $myfile or die "Cannot open file: $myfile\n"; while (<$infh>) { print if /mymatch/; }

    This avoids using a global filehandle, and would allow you to take the filename in as a command line argument, without worrying about escaping nasty characters. I think it's a pretty big benefit for a fairly small change in the code.

Re^2: Searching within compressed files
by Anonymous Monk on Apr 15, 2005 at 16:48 UTC
    That works! I was wrestling with Compress:Zlib, but I kept getting "insufficient memory" errors, even on a much smaller test file.

    Thanks!