in reply to Syslog files revisited
split and sprintf will probably do what you want.
knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > my $line = q{May 2 04:06:15 lon-pop.mail.mydom.com pop3login: LOGOU +T, user=gonenow, ip=[::ffff:127.0.0.1], top=0, retr=0, rcvd=24, sent= +5560, time=1}; > my ( $mon, $day, $time, $dom, $login, $remainder ) = > split m{:?\s+}, $line, 6; > my %monthNos = do { > my $no = 0; > map { $_ => ++ $no } > qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec }; > }; > > my $yr = q{2012}; > my $csv = sprintf q{%02d/%02d/%s %s,%s,%s,%s}, > $day, $monthNos{ $mon }, $yr, $time, $dom, $login, $remainder; > > say $csv;' 02/05/2012 04:06:15,lon-pop.mail.mydom.com,pop3login,LOGOUT, user=gone +now, ip=[::ffff:127.0.0.1], top=0, retr=0, rcvd=24, sent=5560, time=1 knoppix@Microknoppix:~$
I hope this is helpful.
Update: After replying to your reply I realised I had totally missed the need to quote the variable message and add the user and ip to the csv line. Here is revised code.
knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > my $line = q{May 2 04:06:15 lon-pop.mail.mydom.com pop3login: LOGOU +T, user=gonenow, ip=[::ffff:127.0.0.1], top=0, retr=0, rcvd=24, sent +=5560, time=1}; > my ( $mon, $day, $time, $dom, $login, $remainder ) = > split m{:?\s+}, $line, 6; > my %monthNos = do { > my $no = 0; > map { $_ => ++ $no } > qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec }; > }; > > my $yr = q{2012}; > my ( $user, $ip ) = > $remainder =~ m{user=([^,]+),\s+ip=\[([^\]]+)}; > $remainder = qq{"$remainder"}; > my $csv = sprintf q{%02d/%02d/%s %s,%s,%s,%s,%s,%s}, > $day, $monthNos{ $mon }, $yr, $time, $dom, > $login, $remainder, $user, $ip; > > say $csv;' 02/05/2012 04:06:15,lon-pop.mail.mydom.com,pop3login,"LOGOUT, user=gon +enow, ip=[::ffff:127.0.0.1], top=0, retr=0, rcvd=24, sent=5560, time= +1",gonenow,::ffff:127.0.0.1 knoppix@Microknoppix:~$
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Syslog files revisited
by stevbutt (Novice) on Aug 02, 2012 at 23:15 UTC | |
by johngg (Canon) on Aug 03, 2012 at 09:11 UTC | |
by stevbutt (Novice) on Aug 03, 2012 at 16:09 UTC | |
by stevbutt (Novice) on Aug 03, 2012 at 16:18 UTC | |
|
Re^2: Syslog files revisited
by stevbutt (Novice) on Aug 10, 2012 at 13:51 UTC | |
by johngg (Canon) on Aug 10, 2012 at 19:06 UTC |