0

I want the time difference between column 5 & column 4

My Input:

DATE       TIMESTAMP  ID   START TIME END TIME
2019-04-05 13:57:19   1607 13:06:42   13:07:12
2019-04-05 13:58:00   2327 13:57:26   13:57:43

OUTPUT Required:

DATE       TIMESTAMP  ID   START TIME END TIME  TIME DIFFERENCE
2019-04-05 13:57:19   1607 13:06:42   13:07:12  00:00:30
2019-04-05 13:58:00   2327 13:57:26   13:57:43  00:00:17

CODE USED:

awk '
function convert(t) {

    split(t,Arr,":")
    return Arr[1]*3600+Arr[2]*60+Arr[3]

}
/^#/ {print $0,"\ttotal(sec)"; next}
{print $0,"\t",convert($5)-convert($4) }'

OUTPUT OF CODE:

DATE       TIMESTAMP  ID    START TIME END TIME    TIME DIFFERENCE
2019-04-05 13:57:19   1607  13:06:42   13:07:12       30
2019-04-05 13:58:00   2327  13:57:26   13:57:43       17

Output of above given command is in seconds , i need the output in HH:MM:SS.

0

1 Answer 1

2
awk '
function convert(t) {

    split(t,Arr,":")
    return Arr[1]*3600+Arr[2]*60+Arr[3]

}
function HMS(sec){
hr=int(sec/3600)
min=int(sec/60-hr*60)
sec=sec-(hr*3600+min*60)

return sprintf("%02d:%02d:%02d",hr,min,sec)
}
/^#/ {print $0,"\ttotal(sec)"; next}
{print $0,"\t",HMS(convert($5)-convert($4)) }'
1
  • Worked , Solved Commented May 7, 2019 at 12:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.