Reputation: 447
I have a CSV file where dates have the format ddMMMyyyy
or ddMMMyyyy:hh:mm:ss
(the month being English, three-letter, all-caps abbreviations and the other numbers having leading zeros). I have being doing some processing with regex in other files, but I don't know how to transform the month efficiently (without running through the file replacing 12 times). I need to transform everything to a more standard yyyy-mm-dd
(all digits with leading zeros).
Upvotes: 0
Views: 144
Reputation: 69274
You can do this with the Time::Piece module that has been a standard part of Perl since 2007.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $fmt = '%d%b%Y';
$_ = '00039222910,12Jul1999,283.23,290.33,13Jul2013:12:50:01,13Jul2013:12:50:01,AH';
s/(\d\d\w\w\w\d\d\d\d)/convert($1)/ge;
say $_;
sub convert {
my $in = shift;
my $d;
eval { $d = Time::Piece->strptime($in, $fmt) };
return $@ ? $in : $d->ymd;
}
Upvotes: 3