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

Hello PerlMOnks,

I have a comma delimited file with unix time stamps at the start of each line.
I need to parse the file and replace the unix time with standard time.
I can do this one at a time with the perl localtime function from the command line, but as I have thousands of lines to process this could take forever.
I am just learning Perl so any help/pointers would be appreciated...

Replies are listed 'Best First'.
Re: epoch time conversion
by rasta (Hermit) on Dec 10, 2002 at 17:36 UTC
    Look at localtime function. There you will find good explanation how to get values from UNIX-time, and how to use it with strftime function.

    -- Yuriy Syrota
Re: epoch time conversion
by hawtin (Prior) on Dec 10, 2002 at 19:12 UTC

    I need to parse the file and replace the unix time with standard time.

    Does this mean the "web standard" time string? What timezone? (I will assume that you are talking about Gregorian time rather than, for example Julian days or Chinese months).

    I can do this one at a time with the perl localtime function from the command line

    If you have a perl command line to do a single line look at the documentation for the -n and -p switches

Re: epoch time conversion
by BrowserUk (Patriarch) on Dec 10, 2002 at 20:27 UTC

    This may do what you want. See perlrun for an explaination of the switches used and perlvar for the local $,=','; bit.

    perl -F, -ane "local $,=','; print scalar localtime($F[0]), @F[1..$#F] + " <inputfile >outputfile

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      ok this is the output
      # perl -F, -ane "local $,=','; print scalar localtime($F[0]), @F[1..$# +F] + " < small >new Bareword found where operator expected at -e line 1, near "0F" (Missing operator before F?) syntax error at -e line 1, near "0F" Execution of -e aborted due to compilation errors.

        Strange. It works perfectly on my system?

        If your using a *nix system, you may need to swap every occurance of the " char for a ' and vice versa. That's the only thing that comes to mind.

        If that doesn't work maybe one of the *nix guys hereabouts will help you further.

        By way of proof that I did test this, here is a snippet of output from my screen.

        C:\test>perl -F, -ane "local $,=','; print scalar localtime($F[0]), @F +[1..$#F] " <junk.dat Tue Dec 10 19:14:54 2002,some, random, garbage, Tue Dec 10 19:14:57 2002,some, random, garbage, Tue Dec 10 19:15:00 2002,some, random, garbage, Tue Dec 10 19:15:04 2002,some, random, garbage, Tue Dec 10 19:15:07 2002,some, random, garbage, Tue Dec 10 19:15:09 2002,some, random, garbage, Tue Dec 10 19:15:11 2002,some, random, garbage, Tue Dec 10 19:15:13 2002,some, random, garbage, Tue Dec 10 19:15:14 2002,some, random, garbage,

        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: epoch time conversion
by tall_man (Parson) on Dec 10, 2002 at 19:29 UTC
    My impression is that you might be having problems with the mechanics of split and join, so here is a simple example that you could run with "perl -n" that will read from standard input and write to standard output.
    ($t, $rest) = split /,/,$_,2; print join(",",scalar(localtime $t),$rest);
      ok...but how do i feed the file into this? sorry for my lack of knowledge here....