theneil has asked for the wisdom of the Perl Monks concerning the following question:
Perl Gods,
I am generating a CSV file with the first 2 columns in the Epoch time format. I want to be able to run a script that converts and replaces these fields with a human readable date. How would I got about doing that - I'm just learning Perl so if you could explain it to me like I'm 5 that'd be great haha!
CSV:
1345752662, 1345752673, CLOSED, CRITICAL, Other fields etc
What I want:
I'm in the GMT-5 timezone and am not sure how to do that part either. Thanks in advance!Thu Aug 23 2012 15:11:02, Thu Aug 23 2012 15:11:13, CLOSED, CRITICAL, +ETC
***************UPDATE:*****************
Here is the solution I came up with thanks to you guys!
#!/usr/bin/perl use strict; use Text::CSV; use Tie::File; my (@records, @removed); tie @records, 'Tie::File', "test.csv"; @removed = splice(@records,0,2); print join ("\n", @records); untie @records; my $file = 'test.csv'; my $csv = Text::CSV->new(); open (CSV, "<", $file) or die $!; open (MYFILE, '>>convertedDate.csv'); #Column Headers #First 2 are in Epoch time print MYFILE "Arrival, Modified Date, Severity, Status, TicketID, Mess +age\n"; close (MYFILE); while (<CSV>) { if ($csv->parse($_)) { open (MYFILE, '>>convertedDate.csv'); my @columns = $csv->fields(); #I know this next line isn't so elegant print MYFILE scalar(localtime(@columns[0])) . "," . scalar(local +time(@columns[1])) . ",\"" . @columns[2] . "\",\"" . @columns[3] . "\ +",\"" . @columns[4] . "\",\"" . @columns[5] . "\""; print MYFILE "\n"; close (MYFILE); } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } } close CSV;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Epoch time conversion in CSV
by tobyink (Canon) on Sep 25, 2012 at 15:44 UTC | |
|
Re: Epoch time conversion in CSV
by BrowserUk (Patriarch) on Sep 25, 2012 at 15:50 UTC | |
|
Re: Epoch time conversion in CSV
by Marshall (Canon) on Sep 25, 2012 at 16:12 UTC | |
by hokey (Initiate) on Sep 26, 2012 at 02:36 UTC | |
|
Re: Epoch time conversion in CSV
by NetWallah (Canon) on Sep 25, 2012 at 15:35 UTC | |
by theneil (Novice) on Sep 25, 2012 at 15:42 UTC | |
by blue_cowdawg (Monsignor) on Sep 25, 2012 at 15:47 UTC | |
|
Re: Epoch time conversion in CSV
by blue_cowdawg (Monsignor) on Sep 25, 2012 at 15:44 UTC | |
|
Re: Epoch time conversion in CSV
by DrHyde (Prior) on Sep 27, 2012 at 10:06 UTC | |
by Tux (Canon) on Sep 28, 2012 at 11:18 UTC | |
by DrHyde (Prior) on Oct 01, 2012 at 10:20 UTC |