in reply to Re: AM to 24 hours
in thread AM to 24 hours

Hi Let me clarify again hi I have this input in a file .

ID,21,11814,1,11989,150,Y,10/25/2012 11:36:25 PM,TEST

"ID",22,11814,1,11989,150,Y,10-25-2012 11:36:25 PM,TEST

I need to change the time stamp and make the lines look like this . how can I convert this to this using a perl one liner ?

ID,21,11814,1,11989,150,Y,2012-10-25-23.36.25.0000, TEST

ID,22,11814,1,11989,150,Y,2012-10-25-23.36.25.0000, TEST

Thankyou

Replies are listed 'Best First'.
Re^3: AM/PM to 24 hours
by Anonymous Monk on Aug 05, 2013 at 19:21 UTC
    This is my first time on the post so :
    ID,21,11814,1,11989,150,Y,10/25/2012 11:36:25 AM,TEST "ID",22,11814,1,11989,150,Y,10-25-2012 11:36:25 PM,TEST
    I need to change the time stamp and make the lines look like this . how can I convert this to this using a perl one liner ?
    ID,21,11814,1,11989,150,Y,2012-10-25-11.36.25.0000, TEST ID,22,11814,1,11989,150,Y,2012-10-25-23.36.25.0000, TEST
      I took up the challenge to do this as a perl one liner, but this version is much easier to understand and build on in the future:
      use Date::Manip; use Text::CSV; my $csv = Text::CSV->new; while (<>) { $csv->parse($_); my @f = $csv->fields; $f[7] = UnixDate $f[7], "%Y-%m-%d-%H.%M.%S.0000"; $csv->combine(@f); print $csv->string . "\n"; }
      Output:
      ID,21,11814,1,11989,150,Y,2012-10-25-23.36.25.0000,TEST ID,22,11814,1,11989,150,Y,2012-10-25-23.36.25.0000,TEST

      * Should note that the example above isn't kosher as per the documentation. It will fail on embedded newlines and isn't Unicode friendly. Best to consult that documentation if this is anything other than a one-off.

      Here is a simple regular expression to convert a string
      while (<DATA>) { s/(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s+(PM|AM)/$3.'-'.$1.'-'. +$2.'-'.($7 eq 'PM'?($4+12):$4).'.'.$5.'.'.$6.'.0000'/ge; print $_; } __DATA__ ID,21,11814,1,11989,150,Y,10/25/2012 11:36:25 AM,TEST ID,22,11814,1,11989,150,Y,10/25/2012 11:36:25 PM,TEST
      Output:
      ID,21,11814,1,11989,150,Y,2012-10-25-11.36.25.0000,TEST
      ID,22,11814,1,11989,150,Y,2012-10-25-23.36.25.0000,TEST