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

I am new to perl and find difficulty in optimising my code.I have many .gz files and I need to search a pattern in them and return the total number of lines matching the pattern. In bash I can use zgrep <pattern> *.gz | wc -l In perl there is fgrep but it wont work on .gz files.Either I have to uncompress the files and use fgrep or read the compressed files using Compress::Zlib module and check for the pattern.Both are taking very long time(3 times the time taken by zgrep) as I am working on many large gz files . Is there any way of doing this??? Thanks in Advance.

Replies are listed 'Best First'.
Re: grep gz files
by CountZero (Bishop) on Oct 28, 2009 at 07:01 UTC
    Sometimes you just can't beat the utilities provided by the system.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: grep gz files
by Anonymous Monk on Oct 28, 2009 at 06:14 UTC
    Why don't you continue using zgrep?
      I want to know if there is any efficient method in perl which can do this.Using zgrep via system() is same as using bash with perl as a wrapper naa :)
        Using zgrep via system() is same as using bash with perl as a wrapper

        ... where "same" means "fast". I doubt that anything written in Perl will compete with zgrep's speed.

        Using zgrep from perl is a method in perl
Re: grep gz files
by Tanktalus (Canon) on Oct 29, 2009 at 02:06 UTC

    My first thought here is ... PerlIO. Specifically, PerlIO::gzip. The module's synopsis is already pretty close to what you want to do with it anyway. Not guaranteeing it'll be as fast as zgrep, but it may be an improvement over what you're doing now.