[Exim] exigrep: automagically decompress input files (patch)

Top Page
Delete this message
Reply to this message
Author: Steve Haslam
Date:  
To: exim-users
Subject: [Exim] exigrep: automagically decompress input files (patch)
Hi,

The following is a patch that makes exigrep automagically run .gz
files named on its command line through zcat. The advantage here is
that you can say:

exigrep foo@??? /var/log/exim/mainlog*

and it will search all the log files. You can't simply say:

zcat /var/log/exim/mainlog* | exigrep foo@???

since zcat chokes on non-compressed files instead of just passing them
through (unlike zgrep).

The patch keeps intact the behaviour that when no input file is named,
standard input is searched.

It assumes that zcat is on the PATH, which I don't think is too
unhealthy.

HTH

SRH
-- 
Steve Haslam      http://www.arise.demon.co.uk/      steve@???
Debian GNU/Linux Maintainer                               araqnid@???
almost called it today, turned to face the void, numb with the suffering
and the question- "Why am I?"                                  [queensrÿche]

--- /usr/sbin/exigrep    Wed Oct 27 23:31:09 1999
+++ exigrep    Sun Oct 31 22:47:04 1999
@@ -26,11 +26,15 @@
 # input can be a pipe. Program defensively against short lines finding
 # their way into the log.


-while (<>)
- {
- next if length($_) < 20;
+# Instead of just using <>, open each file manually. If the file looks like
+# it's been compressed, filter it through zcat.
+# OTOH, if no files were named, just read STDIN.
+
+# This subroutine processes a single line (in $_) from a log file.
+sub do_line {
+ return if length($_) < 20;
$id = substr($_, 20, 16);
- next if $id !~ /\w{6}\-\w{6}\-\w{2}/;
+ return if $id !~ /\w{6}\-\w{6}\-\w{2}/;
$saved{$id} = '' unless defined($saved{$id});

# Save up the data for this message in case it becomes interesting later.
@@ -42,12 +46,31 @@
# See if this is a completion for some message. If it is interesting,
# print it, but in any event, throw away what was saved.

-  if (/Completed$/)
-    {
+  if (/Completed$/) {
     delete $id_list{$id}, print "$saved{$id}\n" if $id_list{$id};
     delete $saved{$id};
     }
+}
+
+if (@ARGV) {
+  foreach (@ARGV) {
+    my $filename = $_;
+    if ($filename =~ /\.(gz|Z)$/) {
+      open(LOG, "zcat $filename |") || die "Unable to zcat $filename: $!\n";
+    } else {
+      open(LOG, "$filename") || die "Unable to open $filename: $!\n";
+    }
+    while (<LOG>) {
+      do_line;
+    }
+    close(LOG);
+  }
+}
+else {
+  while (<STDIN>) {
+    do_line;
   }
+}


# Print the unCompleted data