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

Hi, I have a log file which has the following format ( its nominally CSV )

John@gmail.com,1268727836,lots,more,fields

I would like to change the second field to a date format the DB I am going to load this into will understand rather than Epoch time i.e.

John@gmail.com,2010/03/16 08:23:56,lots,more,fields

and if possible also add a field at the end of the line containing the original file name i.e.

John@gmail.com,2010/03/16 08:23:56,lots,more,fields,logfile1.log

Whats an efficient way to do this in perl ( there are lots of files with millions of lines ) ? I have found I can generate an individual date which would work with the following code

perl -MPOSIX -le 'print strftime "%F %T", localtime 1268727836'

Its just the looping through the file and running this to sub field 2 in every line that I cant seem to crack - Any help greatly appreciated.

Replies are listed 'Best First'.
Re: Convert Epoch times in Logfile
by NetWallah (Canon) on Jun 08, 2012 at 00:22 UTC
    Expanding your code ..
    perl -MPOSIX -F, -lane 'print join ("," ,$F[0],strftime( "%F %T", loca +ltime $F[1]),@F[2..$#F],$ARGV),"\n"' Your-Log-file.log

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      That works perfectly, thank you so much ! Steve

      perl -MPOSIX -F, -lane 'print join ("," ,$F[0],strftime( "%F %T", loca +ltime $F[1]),@F[2..$#F],$ARGV),"\n"' Your-Log-file.log
      perl -MPOSIX -pe's/^([^,]+,)(\d+)/ $1 . strftime "%F %T", localtime $2 + /e; s/$/,$ARGV/' Your-Log-file.log
Re: Convert Epoch times in Logfile
by locked_user sundialsvc4 (Abbot) on Jun 08, 2012 at 14:32 UTC

    In addition to the foregoing, consider packages such as Date::Calc and Date::Manip.   Packages like these can be useful in situations such as, for example, reliably processing a Unix timestamp-value on a Win32 system, or writing platform-independent code that doesn’t have to care.

    There are also logfile-format specific routines that are intended to simplify processing of these files in a more general sense.   You might discover that even more of your problem has already been solved by others than you might initially have assumed ... which is always a very nice discovery to make.