0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

流行りのAWKで小ネタ。 探せばありそうな気もしますが...

時間の一覧(ラップ等)
00:02:30
00:50:00
00:15:20
01:20:40
合計時間を計算
$ cat time.txt | awk -F: 'BEGIN {h=0; m=0; s=0} {h+=$1; m+=$2; s+=$3} END {h+=int(m/60+s/3600); m=m%60+int(s/60); s%=60; print h":"m":"s}'
2:28:30

コード

3つの部分に分かれています。

まず、BEGIN で変数 h(時), m(分), s(秒) を初期化します。このパターンはプログラム起動時に1度だけ実行されます。

BEGIN {
  h = 0;
  m = 0;
  s = 0;
}

次に、各行で読み込んだ時間を h, m, s に加算します。このタイミングではまだ何も表示しません。

{
  h += $1;
  m += $2;
  s += $3;
}

最後に、全行の集計が終わった段階で END で合計時間を表示します。ms の繰り上がりを補正してから表示しています。

END {
  h += int(m / 60 + s / 3600);
  m = m % 60 + int(s / 60);
  s %= 60;
  print h":"m":"s;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?