eran eran - 1 year ago 132
Linux Question

Convert date to epoch time using AWK in linux

I have a comma-separated file, with the first column as a date of the format 01/31/2010
that I want to change into epoch time, such that the file "file.csv":


will change into "output.csv":


I know the command line date -d "01/30/2010" +%s will work, but only on a single date, and I need to feed it into a table, so, is there a way to use awk with some func():

cat file.csv | awk -F, 'print func($1)","$2","$3}'

Since I don't really care how I do this, alternatively, how would I change a date in excel into epoch, when the string is mm/dd/yyyy...

Answer Source
TZ=PST awk -F, '{split($1,date,"/");
                 $1=mktime(date[3] " " date[1] " " date[2] " " "00 00 00");

Or, invoking date:

TZ=PST awk -F, '{ OFS = FS;
                  command="date -d" $1 " +%s";
                  command | getline $1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download