darxide1982 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to pull a list of the email addresses that are being attempted on my mail server as a result of myDoom going about. I have:
cat /var/log/mail/info | perl -n -e 'print if /450 </ .. />/'
Which will give me all the lines with the 450: <emailaddress> content, but it gives me all the line. How do I make it give me just the email address?? Many thanks.

Replies are listed 'Best First'.
Re: Extracting Pattern Match from log
by rob_au (Abbot) on Jan 28, 2004 at 11:32 UTC
    Whilst I don't have any logs with myDoom virus attempts to test with, this should do the trick for you ...

    perl -MEmail::Valid::Loose -nle 'print $1 if /450 <($Email::Valid::Loo +se::Addr_spec_re)>/' < /var/log/mail/info

    Or if you really want quick-and-dirty with a very loosely matching regular expression ...

    perl -nle 'print $1 if /450 <([^\>]+)>/' < /var/log/mail/info

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001010111010'"

      Works great, thanks :)
Re: Extracting Pattern Match from log
by borisz (Canon) on Jan 28, 2004 at 11:36 UTC
    This grabs anything between < and > on a line with 450 in it, and print it. I'm not sure if this is what you want to do.
    cat /var/log/mail/info | perl -n -e 'print $1, $/ if /450 <([^>]*)>/'
    Boris

      Ladies and gentlemen, we have a new nominee for the Useless Use of Cat Award ! ;)

      This would do exactly the same, yet without abusing cat:

      perl -n -e 'print $1, $/ if /450 <([^>]*)>/' /var/log/mail/info
      --
      b10m

      All code is usually tested, but rarely trusted.
Re: Extracting Pattern Match from log
by b10m (Vicar) on Jan 28, 2004 at 12:22 UTC

    ...foolish mortal Perl Monks ... ;) If you like sed, here's a way to do it, for TIMTOWTDI:

    $ sed -n 's/^.*450 <\([^>]*\)>.*$/\1/p' /var/log/mail/info

    Not that it's faster, but it's different :)

    --
    b10m

    All code is usually tested, but rarely trusted.

      Wow, definately not faster here (dual p4 box, RedHat 9).

      sed -n 's/^.*450: <\([^>]*\)>.*$/\1/p' bar > /dev/null 57.37s user 0.08s system 99% cpu 58.002 total perl -lne 'print $1 if /450: <([^>]+)/' bar > /dev/null 6.95s user 0.09s system 98% cpu 7.164 total ruby -ne 'puts $1 if /450: <([^>]+)/' bar > /dev/null 10.46s user 0.09s system 96% cpu 10.939 total tarsier:~ 602> wc -l bar ; head -4 bar 2430000 bar blah de bla 450: <zorch@example.com> woop blah de bla 450: <blink@example.com> woop blah de bla 450: <zorch@example.com> woop blah de bla 450: <blink@example.com> woop
Re: Extracting Pattern Match from log
by pelagic (Priest) on Jan 28, 2004 at 11:38 UTC
    You can assign the list of matched strings to an array.
    try this
    @lookingfor = $_ =~ /450 <([^>]*)>/;
    you might want to replace the regx by something more reasonable ...
    Imre