# Calculate the time difference between two columns in time format hh:mm:ss

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.

``````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)) }'
``````