Sridhar-Sarnobat Sridhar-Sarnobat - 1 month ago 5x
Perl Question

Date to epoch conversion for regex group in perl one liner?

I have some csv data looking like this:

player A,1991,2006
player B,1958,1972

and I want to transform it to use epochs instead of years so that it looks like:

player A,1329638400,162008400
player B,1326389534,326359832

I'd prefer to use the following perl idiom:

cat player_data.csv | perl -pe 's{...}{...}g'

But trying to call the relevant perl function inside a regex I'm not experienced with.


  • I'm fine with hardcoding the month and day to something (preferably july 1st which is the start of new soccer seasons and I'm trying to capture data about their careers)


This should do it:

perl -pe 'use Time::Local; s/(\d{4})/timelocal(0,0,0,1,0,$1)/ge' player_data.csv

Note this assumes that you want January 1st 00:00 in local time. If you want UTC instead, just use timegm instead of timelocal.