tags:

views:

40

answers:

2

I've currently got a bash script that parses /var/log/mail.log to determine the last login time/date of a imap user. I've determined Perl is going to be a lot more efficient and quicker for this task, especially as the logs grow.

Given the following example /var/log/mail.log file:

Jul  5 06:57:54 mail-04 dovecot: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=192.168.x.x, lip=192.168.x.x
Jul  5 06:59:54 mail-04 dovecot: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=192.168.x.x, lip=192.168.x.x
Jul  5 06:59:59 mail-04 dovecot: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=192.168.x.x, lip=192.168.x.x
Jul  5 07:01:54 mail-04 dovecot: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=192.168.x.x, lip=192.168.x.x
Jul  5 07:01:59 mail-04 dovecot: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=192.168.x.x, lip=192.168.x.x

What is the most efficient way in Perl to print the last login time of each unique user? E.g. the expected output should be:

[email protected] last imap-login: Jul 5 07:01:54
[email protected] last imap-login: Jul 5 07:01:59
+2  A: 

Using a hash and keep assigning the login time to the user name sounds like a simple solution.

$logins{$username} = $date
OneOfOne
+1  A: 
perl -ne '$l{$2}=$1 if /^(.{15}) .* imap-login: Login: user=<([^>]+)>/; END { print "$_ last imap-login: $l{$_}\n" for keys %l }' /var/log/mail.log
dolmen