31
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Linux][awk] awkでログ集計(合計, 平均)

Posted at

ログ集計時にたまに使って毎回調べてるのでメモ

入力例

test.log
A 1.1 funcA1()
B 1.2 funcB1()
C 1.3 funcC1()
A 2.4 funcA2()
B 2.5 funcB2()
C 2.6 funcC2()

使用例

合計

すべての合計
$ cat test.log | awk '{sum+=$2} END {print sum}'
11.1
Bの合計
$ cat test.log | awk '/^B\s.*$/{sum+=$2} END {print sum}'
3.7

平均

すべての平均
$ cat test.log | awk '{sum+=$2} END {print sum/NR}'
1.85
Aの平均
$ cat test.log | grep -E "^A\s.*$" | awk '{sum+=$2} END {print sum/NR}'
1.75

(抽出した場合の個数を簡単に出す方法がわからなかったので、前段でgrepを使ってます・・・)

参考

Awkワンライナー (最小値、最大値、合計、平均、中央値) - それがしの足音
統計屋のためのAWK入門 - あんちべ!

31
22
1

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
31
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?