Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

opening/grepping a gzipped file

by Ronunism (Novice)
on Dec 19, 2001 at 01:23 UTC ( #132936=perlquestion: print w/ replies, xml ) Need Help??
Ronunism has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to open up and grep a gzipped log file, but can't seem to ever get it to work.
Currently I have:
open (FILE, `gunzip -dc /usr/local/apache/logs/access.log.1.gz | grep +"dd/Mon/yyyy" |`) or die "Couldn't open gunzip pipe ($!)\n"; while (<FILE>) { # massive substitutions and DBI Calls } close FILE;

and get a "Couldn't open gunzip pipe (No such file or directory)" error. When I remove the grep call, I get an "Couldn't open gunzip pipe (File name too long)" error. Any ideas on what I'm doing wrong?

Comment on opening/grepping a gzipped file
Download Code
Re: opening/grepping a gzipped file
by sparkyichi (Deacon) on Dec 19, 2001 at 01:38 UTC
    Try the Zip modules for Archive such as Archive::Zip and Archive::Tar.

    Sparky
Re: opening/grepping a gzipped file
by tune (Curate) on Dec 19, 2001 at 01:40 UTC
    Why are you using ` to open a pipe? Why not "?
    Maybe
    open (FILE, "gunzip -dc /usr/local/apache/logs/access.log.1.gz | grep +\"dd/Mon/yyyy\" |") or die "Couldn't open gunzip pipe ($!)\n";
    will be better

    --
    tune

Re: opening/grepping a gzipped file
by TomK32 (Monk) on Dec 19, 2001 at 01:50 UTC
    Un*x User might prefer zgrep or one of the other *greps:
    tomk32@tomk32 ~$ apropos grep bzegrep (1) - search possibly bzip2 compressed files for a re +gular expression bzfgrep (1) - search possibly bzip2 compressed files for a re +gular expression bzgrep (1) - search possibly bzip2 compressed files for a re +gular expression egrep (1) - print lines matching a pattern exigrep (8) - program to extract information from exim logs fgrep (1) - print lines matching a pattern grep (1) - print lines matching a pattern pgrep (1) - look up or signal processes based on name and o +ther attributes rgrep (1) - print lines matching a pattern zegrep (1) - search possibly compressed files for a regular +expression zfgrep (1) - search possibly compressed files for a regular +expression zgrep (1) - search possibly compressed files for a regular +expression zipgrep (1) - search files in a ZIP archive for lines matchin +g a pattern

    -- package Lizard::King; sub can { do { 'anything'} };
Re: opening/grepping a gzipped file
by dws (Chancellor) on Dec 19, 2001 at 01:59 UTC
    First off, don't use backticks. If you do, the filename passed to open() might be very, very long.

    Secondly, you can save a process invocation by doing the grep inline.

    my $file = "/usr/local/apache/logs/access.log.1.gz"; open(PIPE, "gunzip -dc $file |") or die "gunzip $file: $!"; while ( <PIPE> ) { next if ! m|dd/Mon/yyyy|; # massive whatever } close(PIPE);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://132936]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-09-17 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls