我想要第 5 列和第 4 列之间的时间差
我的输入:
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
所需输出:
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
使用的代码:
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) }'
代码输出:
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
上面给定命令的输出以秒为单位,我需要 HH:MM:SS 的输出。
答案1
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)) }'