in reply to Process mail logs

Perhaps Perl's (5.10+) given/when will work for you:

use Modern::Perl; while ( my $string = <DATA> ) { given ($string) { when (/<=/) { say '<= was found.'; } when (/=>/) { say '=> was found.'; } when (/==/) { say '== was found.'; } when (/\*\*/) { say '** was found.'; } default { say "The following was found: $_"; } } } __DATA__ May 2 07:06:20 lon.mail.net exim[1234]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e <= it_ndt_bounces@new.itunes.com H=smtpmail.com [21.5.10.4] I +=[8.4.14.4]:25 P=esmtp S=1966 id=1603882764.112965659.1335927964793.M +ail.cboxp@ednabay.apple.com T="New on iTunes: One Thing And, Then Ano +ther, Cooking Apps,\n Great Deals on First Seasons, and M" May 2 07:06:20 lon.mail.net exim[1234]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e <= it_ndt_bounces@new.itunes.com H=smtpmail.com [21.5.10.4] I +=[8.4.14.4]:25 P=esmtp S=1966 id=1603882764.112965659.1335927964793.M +ail.cboxp@ednabay.apple.com T="New on iTunes: One Thing And, Then Ano +ther, Cooking Apps,\n Great Deals on First Seasons, and M" May 2 07:06:20 lon.mail.net exim[1235]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e => peterpiper <peterpiper@nosuchdomain.net> R=local_mail T=lo +cal_maildir_mail_drop May 2 07:06:20 lon.mail.net exim[1235]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e == peterpiper <peterpiper@nosuchdomain.net> R=local_mail T=lo +cal_maildir_mail_drop May 2 07:06:20 lon.mail.net exim[1234]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e ** it_ndt_bounces@new.itunes.com H=smtpmail.com [21.5.10.4] I +=[8.4.14.4]:25 P=esmtp S=1966 id=1603882764.112965659.1335927964793.M +ail.cboxp@ednabay.apple.com T="New on iTunes: One Thing And, Then Ano +ther, Cooking Apps,\n Great Deals on First Seasons, and M" May 2 07:06:20 lon.mail.net exim[1235]: 2012-05-02 07:06:20 1PSPtU-00 +04en-1e -- peterpiper <peterpiper@nosuchdomain.net> R=local_mail T=lo +cal_maildir_mail_drop

Output:

<= was found. <= was found. => was found. == was found. ** was found. The following was found: May 2 07:06:20 lon.mail.net exim[1235]: 2012 +-05-02 07:06:20 1PSPtU-0004en-1e -- peterpiper <peterpiper@nosuchdoma +in.net> R=local_mail T=local_maildir_mail_drop

Hope this helps!

Update: My thanks to influx for pointing me to an article in which the author encourages programmers to Use for() instead of given(). This may be especially relevant in this case, where millions of lines could be processed.

Replies are listed 'Best First'.
Re^2: Process mail logs
by influx (Beadle) on Aug 13, 2012 at 07:45 UTC

    Actually, you should try and refrain from using given/when. Brian D Foy explains why Here

    A pretty nice alternative I use, is

    for ($string) { # given if (/<=/) { .. } # when elsif (/=>/) { .. } # when else { .. } # default }

      Good call to address this given/when issue. Have updated my original post to reflect this...

Re^2: Process mail logs
by stevbutt (Novice) on Aug 13, 2012 at 10:41 UTC

    Unfortunately this has to run on a stock Solaris 10 machine and it only comes with Perl 5.8.4

    Is there another way ?

      Certainly. The given/when (or for/when) construct is cleaner, but there's no reason you can't fall back on a series of if/elsif/else. (Though I'd recommend looking at the dispatch table concept first.) In your sample data, the fields appear consistent up to the 'directional' field you're looking at, so you could:

      while(<DATA>){ chomp; my( $month, $day, $time, $host, $ppid, $date, $time2, $something, $d +irection, $remainder ) = split ' ', $_, 10; if( $direction eq '<=' ){ # go left } elsif( $direction eq '=>' ){ # go right } elsif( $direction eq '==' ){ # do a third thing } elsif( $direction eq '**' ){ # do a fourth thing } else { # fall back on a default behavior } }

      Aaron B.
      Available for small or large Perl jobs; see my home node.