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