Looking to convert human readable timestamps to epoch/Unix time within a CSV file using GAWK in preparation for loading into a MySQL DB.
Data Example:
{null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647
Looking to take column 6, Tuesday, November 26 12:17 PM, and convert to epoch time for storage. All times shown will be in EST format. I realize AWK is the tool for this, but can't quite seem to structure the command. Currently have:
cat FILE_IN.CSV | awk 'BEGIN {FS=OFS=";"}{$6=strftime("%s")} {print}'
However this returns:
{null};2013-11-26;Text & Device;Location;/file/path/to/;1385848848;1;1385845647
Presumably, this means I'm calling the current epoch time (1385848848 was current epoch at time of execution) and not asking strftime
to convert the string; but I can't imagine another way to doing this.
What is the proper syntax for gawk
/strftime
to convert an existing timestamp to epoch?
Edit: This question seems loosely related to How do I use output from awk in another command?
Here's how to generally convert a date from any format to seconds since the epoch using your current format as an example and with comments to show the conversion process step by step:
I'm sure you can modify that for the current problem.
Also, if you don't have gawk you can write the cvttime() function as (borrowing @sputnik's
date
command string):I left srtftime() in there just to show that the secs was correct - replace with
date
as you see fit.For the non-gawk version, you just need to figure out how to get the year into the input month/date/time string in a way that
date
understands if that maters to you - shouldn't be hard.You can convert date to epoch with this snippet :
So finally :
Thanks @Keiron for the snippet.