Hi, Thanks, that seems a far better way to do it. A few things though:
I am not 100% sure what this line does.
push @{ $msgids->{$1} }, $_;
Is the @{} surrounding the hash used to make push work with a hash? Also, why do you need -> and can't just do $msgids{$1}, $_; ?
I made a slight change to make your addition work in that I added ()
around the regex so that $1 would work - is there any reason to use this over $&?
So, AFAICS, your addition creates a hash (Using the message id's as the key) and puts the associated line from the log in the hash. So I thought I could simply do:
for (sort keys %msgids) { print $msgids{$_}; }
To print each line found. For some reason this returns nothing.
Thanks for your help. Hope my questions aren't too simple!
Nick
[EMAIL PROTECTED] wrote:
----- Original Message ----- From: Nick Chettle <[EMAIL PROTECTED]> Date: Wednesday, March 16, 2005 7:02 am Subject: Perl Analyzing Maillog
Hi All,
Hello,
I am trying to write a simple script that will analyze a Postfix maillog. The basic idea is that you give it an e-mail address and it will print all the relevant lines. In order to achieve this, you first need a message ID, you can then search for the id and bring all the relevant information.
This is what I have so far:
#!/usr/bin/perl
print "Please enter an e-mail address: "; chomp($email = <STDIN>);
open MAILLOG, "/var/log/maillog";
while (<MAILLOG>) { if (/$email/) { if (/[A-Z1-9]{8}/) { $msgids[$_] = $&; } } }
This works fine and it builds an array of all the message id's. I now need an efficient way of searching through the log again and pulling out all lines that contain a message id.
You are already doing it, why not keep the results first time around ?? One was it to create a Ref to Hash Ref of Array's [ I prefer this, others may simply do a HoA ]
while (<MAILLOG>) {
if (/$email/) {
if (/[A-Z1-9]{8}/) {
push @{ $msgids->{$1} }, $_; }
}
}
I can do this with:
for (@msgids) { system "cat /var/log/maillog | grep $_"; }
But it's certainly not a very Perl way of doing it and it's less than efficient as I have to cat the entire maillog for each message id.
I've been trying to open the filehandle again and then somehow get foreach to go through it but haven't had much sucess. Can anyone advise if I'm looking along the right lines or if there is a better way of doing it?
Thanks, Nick
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>