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

Hi Monks, I'm trying to get the IP address from the following format log file for pop-before-smtp perl script. The regex syntax provided there is not working for me actually.
2011.05.13-14:54:58, test@xyz.com, 180.211.51.11, pop3 2011.05.13-14:57:26, test@xyz.com, 160.234.47.12, pop3 2011.05.13-14:57:54, www@abcd.com, 166.22.22.224, imap 2011.05.13-14:57:55, www@somedomain.com, 172.58.22.154, imap 2011.05.13-15:03:08, www@example.com, 190.22.120.44, pop3
Kindly advice me the regex to get only the ip address from each line. Thanks for your help in advance.

Replies are listed 'Best First'.
Re: regex for perl script
by Utilitarian (Vicar) on May 15, 2011 at 16:24 UTC
    You don't need a regex to extract the IP from that log, it's always in the same position
    while(<$logfile>){ push @ip_addresses, (split / /, $_)[2]; }

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Almost; but that will retain the ',' after the IP-address; use
      split /, /
      instead.

      Paul

        Thanks for you feedbacks dude. I'm sorry to say that i'm unable to fit the code into the script. I'm novice to perl. Here's the full script:

        I've another dovecot log file which also logs login info. If the script extract rip from this log that's also fine:
        2011-05-15 16:10:10 pop3-login: Info: Login: user=<test@example.com>, +method=PLAIN, rip=180.234.57.184, lip=166.165.52.15 2011-05-15 16:10:11 POP3(test@example.com): Info: Disconnected: Logged + out top=0/0, retr=0/0, del=0/0, size=0 2011-05-15 16:10:14 pop3-login: Info: Login: user=<test@example.com>, +method=PLAIN, rip=180.234.57.184, lip=166.165.52.15 2011-05-15 16:10:15 POP3(test@example.com): Info: Disconnected: Logged + out top=0/0, retr=0/0, del=0/0, size=0 2011-05-15 16:10:17 pop3-login: Info: Login: user=<test@example.com>, +method=PLAIN, rip=180.234.57.184, lip=166.165.52.15 2011-05-15 16:10:18 POP3(test@example.com): Info: Disconnected: Logged + out top=0/0, retr=0/0, del=0/0, size=0
Re: regex for perl script
by Khen1950fx (Canon) on May 15, 2011 at 21:26 UTC
    Personally, I don't use regexen with IP addresses. There's a node that was written in 2002 that covers some of the pitfalls that you'll encounter. See:

    Don't Use Regular Expressions to Parse IP Addresses!

    While using regexen for server logfiles is OK, they can turn on you.
Re: regex for perl script
by AnomalousMonk (Archbishop) on May 15, 2011 at 23:00 UTC

    When you finish negotiating with your consultant to alter your script, you might suggest he or she investigate Regexp::Common and Regexp::Common::net for IPv4 addresses.

Re: regex for perl script
by CountZero (Bishop) on May 16, 2011 at 06:36 UTC
    Things like "The regex syntax provided there is not working for me actually" (without even given the regex that is not working) and "i'm unable to fit the code into the script" (followed by 643 lines of code you have not written yourself), will not get you any useful answer here.

    Why don't you contact the author of that code?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I've got it working.
      '^(\d\d\d\d-\d+-\d+ \d+:\d+:\d+) (?:imap|pop3)-login: Info: ' . 'Login: .*? (?:\[|rip=)[:f]*(\d+\.\d+\.\d+\.\d+)[],]';
      Thanks.
        Just in case the regex gets crazy on you, fall back to Text::CSV::Simple.
        #!/usr/bin/perl use strict; use warnings; use Text::CSV::Simple; use Data::Dump qw(dump); my $logfile = "/root/Desktop/ip.log"; my $parser = Text::CSV::Simple->new; $parser->want_fields(2); my @data = $parser->read_file($logfile); print dump(@data);