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:
Thu Aug 23 2012 15:11:02, Thu Aug 23 2012 15:11:13, CLOSED, CRITICAL,
+ETC
I'm in the GMT-5 timezone and am not sure how to do that part either.
Thanks in advance!
***************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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.