Timestamp to Epoch in a CSV file with GAWK

后端 未结 2 1266
抹茶落季
抹茶落季 2020-12-18 05:07

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:



        
相关标签:
2条回答
  • 2020-12-18 05:16
    $ 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.

    0 讨论(0)
  • 2020-12-18 05:24

    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.

    0 讨论(0)
提交回复
热议问题