如何总结运行时间

时间:2014-03-24 19:48:43

标签: bash tcl

我想总结一些脚本执行时间。我尝试通过tcl来做到这一点。 作为输入,我有:

00:00:00:09
00:00:34:05
00:00:50:34
.....

输入格式为<days:hours:minutes:seconds>

如何总结时间并将一个输出作为汇总输出。

输出格式:<days:hours:minutes:seconds>

2 个答案:

答案 0 :(得分:2)

使用名为“durations”的文件:

set days [set hours [set mins [set secs 0]]]
set fh [open durations r]
while {[gets $fh line] != -1} {
    scan $line %d:%d:%d:%d d h m s
    incr days $d
    incr hours $h
    incr mins $m
    incr secs $s
}
close $fh

incr mins  [expr {$secs / 60}];  set secs  [expr {$secs % 60}]
incr hours [expr {$mins / 60}];  set mins  [expr {$mins % 60}]
incr days  [expr {$hours / 24}]; set hours [expr {$hours %24}]
puts [format "%d:%02d:%02d:%02d" $days $hours $mins $secs]
0:01:24:48

和bash

while IFS=: read d h m s; do
    (( days += 10#$d, hours += 10#$h, mins += 10#$m, secs += 10#$s ))
done < durations 
(( mins += secs/60, secs %= 60 ))
(( hours += mins/60, mins %= 60 ))
(( days += hours/24, hours %= 24 ))
printf "%d:%02d:%02d:%02d\n" $days $hours $mins $secs
0:01:24:48

bash while循环中的10#是对前导零的数字强制执行10的基数解释(避免08,09的非法八进制错误)

答案 1 :(得分:1)

echo "00:00:00:09
00:00:34:05
00:00:50:34" | \
awk -F ':' '
  BEGIN{s=0}
  {s += $4 + $3*60 + $2*3600 + $1*86400}
  END{
    rem = s%86400
    days = (s-rem)/86400
    s = rem

    rem = s%3600
    hours = (s-rem)/3600
    s = rem

    rem = s%60
    mins = (s-rem)/60
    s=rem

    printf("%02d:%02d:%02d:%02d\n", days, hours, mins, s)
  }'

输出:

00:01:24:48