Sid
Sid

Reputation: 171

awk to convert unix time without strftime or date command(-d & -r option isn't supported in AIX)

I have current output as

file | awk '$1=="IMAGE" {print $2","$7","$14","$17","$19/1000}'

ABC,POL1,1510009167,1510009677,20.5
DEF,POL2,1510009667,1510009887,20.5
XYZ,POL3,1510007867,1510009887,20.5
PQR,POL4,1510009267,1510009997,20.5

Expected output should have these unix time converted into human readable format, I can't use strftime or date -d option as they are not supported in AIX. I do have a user defined command which i can use to convert but i can't use it in awk.I tried creating function and then use it in awk which didn't help

With Ed's Code I am getting this output as the user defined command gives output like this, However I want it to give me only day/Mon/Year. I can use awk to print this in user command, But i do i use it in this function?

ABC POL1 1510009167 = Mon Nov  6 17:59:27 EST 2017 1510009677 = Tue Nov  7 04:37:57 IST 2017 20.5
DEF POL2 1510009667 = Mon Nov  6 18:07:47 EST 2017 1510009887 = Tue Nov  7 04:41:27 IST 2017 20.5
XYZ POL3 1510007867 = Mon Nov  6 17:37:47 EST 2017 1510009887 = Tue Nov  7 04:41:27 IST 2017 20.5
PQR POL4 1510009267 = Mon Nov  6 18:01:07 EST 2017 1510009997 = Tue Nov  7 04:43:17 IST 2017 20.5

Upvotes: 1

Views: 1198

Answers (3)

Ed Morton
Ed Morton

Reputation: 203899

Assuming your user defined command that you mention is in an executable named "command" that's in your PATH which you today call as:

command -ctime $14 | awk '{print $4"/"$5"/"$6"/"}

to get the format you want (per your comments) then all you need is to write an awk function to call it:

awk '
function secs2time(secs,     cmd, line, flds, time) {
    cmd = "command -ctime \"" secs "\""
    if ( (cmd | getline line) > 0  ) {
        split(line,flds," ")
        time = flds[4] "/" flds[5] "/" flds[6] "/"
    }
    else {
        time = "error:" secs
    }
    close(cmd)
    return time
}
BEGIN { FS=OFS="," }
$1=="IMAGE" {print $2, $7, secs2time($14), secs2time($17), $19/1000}
'

Upvotes: 1

karakfa
karakfa

Reputation: 67497

if you already have a date conversion routine, wrap it in a command and call from awk

using your data

$ awk -F, '{"./todate.sh " $3 | getline $3}1' file

ABC POL1 Mon Nov  6 17:59:27 EST 2017 1510009677 20.5
DEF POL2 Mon Nov  6 18:07:47 EST 2017 1510009887 20.5
XYZ POL3 Mon Nov  6 17:37:47 EST 2017 1510009887 20.5
PQR POL4 Mon Nov  6 18:01:07 EST 2017 1510009997 20.5

where the date conversion I used is

==> todate.sh <==

#!/bin/bash    
date -d "@$1"

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247002

GNU awk has a builtin strftime() function, but that most likely won't be available to you on AIX.

Try perl instead of awk:

perl -MPOSIX=strftime -F, -ane '
    $F[2] = strftime("%Y-%m-%dT%H:%M:%S", localtime($F[2]));
    $F[3] = strftime("%Y-%m-%dT%H:%M:%S", localtime($F[3]));
    print join ",", @F
' <<END
ABC,POL1,1510009167,1510009677,20.5
DEF,POL2,1510009667,1510009887,20.5
XYZ,POL3,1510007867,1510009887,20.5
PQR,POL4,1510009267,1510009997,20.5
END
ABC,POL1,2017-11-06T17:59:27,2017-11-06T18:07:57,20.5
DEF,POL2,2017-11-06T18:07:47,2017-11-06T18:11:27,20.5
XYZ,POL3,2017-11-06T17:37:47,2017-11-06T18:11:27,20.5
PQR,POL4,2017-11-06T18:01:07,2017-11-06T18:13:17,20.5

Upvotes: 1

Related Questions