Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp to Epoch in a CSV file with GAWK

Tags:

csv

epoch

awk

gawk

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?

like image 480
Keiron Avatar asked Nov 30 '13 22:11

Keiron


2 Answers

$ cat file
{null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647

$ gawk 'BEGIN{FS=OFS=";"} {gsub(/-/," ",$2); $2=mktime($2" 0 0 0")}1' file
{null};1385445600;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647

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:

$ cat tst.awk
function cvttime(t,     a) {
    split(t,a,/[,: ]+/)
    # 2013 Tuesday, November 26 10:17 PM
    #  =>
    #    a[1] = "2013"
    #    a[2] = "Tuesday"
    #    a[3] = "November"
    #    a[4] = "26"
    #    a[5] = "10"
    #    a[6] = "17"
    #    a[7] = "PM"

    if ( (a[7] == "PM") && (a[5] < 12) ) {
        a[5] += 12
    }
    # => a[5] = "22"

    a[3] = substr(a[3],1,3)
    # => a[3] = "Nov"

    match("JanFebMarAprMayJunJulAugSepOctNovDec",a[3])
    a[3] = (RSTART+2)/3
    # => a[3] = 11

    return( mktime(a[1]" "a[3]" "a[4]" "a[5]" "a[6]" 0") )
}

BEGIN {
    mdt ="Tuesday, November 26 10:17 PM"
    secs = cvttime(2013" "mdt)
    dt = strftime("%Y-%m-%d %H:%M:%S",secs)
    print mdt ORS "\t-> " secs ORS "\t\t-> " dt
}
$ awk -f tst.awk
Tuesday, November 26 10:17 PM
        -> 1385525820
                -> 2013-11-26 22:17:00

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):

$ cat tst2.awk
function cvttime(t,     cmd,secs) {
    cmd = "date -d \"" t "\" '+%s'"
    cmd | getline secs
    close(cmd)
    return secs
}

BEGIN {
    mdt ="Tuesday, November 26 10:17 PM"
    secs = cvttime(mdt)
    dt = strftime("%Y-%m-%d %H:%M:%S",secs)
    print mdt ORS "\t-> " secs ORS "\t\t-> " dt
}
$
$ awk -f tst2.awk
Tuesday, November 26 10:17 PM
        -> 1385525820
                -> 2013-11-26 22:17:00

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.

like image 95
Ed Morton Avatar answered Oct 02 '22 03:10

Ed Morton


You can convert date to epoch with this snippet :

$ date -d 'Tuesday, November 26 12:17 PM' +%s
1385464620

So finally :

awk -F";" '{system("date -d \""$6"\" '+%s'")}' file

Thanks @Keiron for the snippet.

like image 43
Gilles Quenot Avatar answered Oct 02 '22 02:10

Gilles Quenot