Little help with While loop please?

A

Allister

I'm trying to take in a log file via stdin from my mail server. The
mail server will output anywhere from 5-11 lines in the log file per
message. I need to parse these lines looking for the first word of
FROM so I can get the senders email address. Then I need to walk a
few more lines down looking for BDAT or DATA to get the size of the
message where 3333 is the message size below. Then just output these
two out to STDOUT on the same line.

I've currently got a while (<>) {} loop to parse things one at a time,
but I can't figure out how to make it go down a few more lines looking
for the BDAT or DATA part without losing the information from the line
where I got the FROM data.

Here is a sample of the log I'm parsing:
date time ip1 fqdn ip2 EHLO +hotmail.com 250 0 314 16
date time ip1 fqdn ip2 MAIL +FROM:<[email protected]> 0 0 45 32
date time ip1 fqdn ip2 RCPT +TO:<[email protected]> 250 0 29 26
date time ip1 fqdn ip2 BDAT +<[email protected]> 250 0 79 3333
date time ip1 fqdn ip2 QUIT hotmail.com 240 6359 72 4

Does anyone have any example code or anything that I could look at to
do this? If I've left out any info, please let me know and I'll post
right away.

Thanks,

-Jaime
 
J

James Willmore

Does anyone have any example code or anything that I could look at
to do this? If I've left out any info, please let me know and I'll
post right away.

The code:

#!/usr/bin/perl -w

use strict;

while(<DATA>){
print "$1 " if m/FROM:<(.*)>/;
print "$2\n" if m/(BDAT|DATA) .* (\d+)$/;
}

exit;

__DATA__
date time ip1 fqdn ip2 EHLO +hotmail.com 250 0 314 16
date time ip1 fqdn ip2 MAIL +FROM:<[email protected]> 0 0 45 32
date time ip1 fqdn ip2 RCPT +TO:<[email protected]> 250 0 29 26
date time ip1 fqdn ip2 BDAT +<[email protected]> 250 0 79 3333
date time ip1 fqdn ip2 QUIT hotmail.com 240 6359 72 4

The results:

jim@maxine:~> perl news.pl
(e-mail address removed) 3333
jim@maxine:~>

Work for you?

HTH

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top