Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Re: Syslog files revisited by johngg
in thread Syslog files revisited by stevbutt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2014-10-24 10:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (131 votes), past polls