16
13

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.

簡単にShell上でバーグラフを表示するためのワンライナー

Last updated at Posted at 2016-10-14

サーバーでのログ解析の結果を視覚的にしたい場合に便利です。

$ echo -ne "hoge 4\nfuga 5\npiyo 6" | perl -anle 'print "$F[0] "."▇"x$F[1]." $F[1]"'
hoge ▇▇▇▇ 4
fuga ▇▇▇▇▇ 5
piyo ▇▇▇▇▇▇ 6

大きい数字のときは雑に割り算してやればいい。

$ echo -ne "hoge 69\nfuga 511\npiyo 1195" | perl -anle 'print "$F[0] "."▇"x($F[1]/30)." $F[1]"'
hoge ▇▇ 69
fuga ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 511
piyo ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1195

実際にログ解析してみる(圧縮されているLTSVな1日分のログ)

$ zcat access.log-20161018 | grep login | perl -F'\t' -nale '%h=map{split/:/,$_,2}@F;print"$h{time}"' | cut -c8-14 | sort | uniq -c
10 2016:00
5 2016:01
0 2016:02
0 2016:03
2 2016:04
0 2016:05
0 2016:06
4 2016:07
5 2016:08
50 2016:09
100 2016:10
120 2016:11
150 2016:12
300 2016:13
250 2016:14
150 2016:15
100 2016:16
40 2016:17
40 2016:18
140 2016:19
300 2016:20
100 2016:21
100 2016:22
50 2016:23

などとして、1時間ごとのアクセス数を出すとする。これの末尾に perl -anle 'print "$F[1] "."▇"x($F[0]/5)." $F[0]"' を追加してやると

$ zcat access.log-20161018 | grep login | perl -F'\t' -nale '%h=map{split/:/,$_,2}@F;print"$h{time}"' | cut -c8-14 | sort | uniq -c | perl -anle 'print "$F[1] "."▇"x($F[0]/5)." $F[0]"'
2016:00 ▇▇ 10
2016:01 ▇ 5
2016:02  0
2016:03  0
2016:04  2
2016:05  0
2016:06  0
2016:07  4
2016:08 ▇ 5
2016:09 ▇▇▇▇▇▇▇▇▇▇ 50
2016:10 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100
2016:11 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 120
2016:12 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 150
2016:13 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 300
2016:14 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 250
2016:15 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 150
2016:16 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100
2016:17 ▇▇▇▇▇▇▇▇ 40
2016:18 ▇▇▇▇▇▇▇▇ 40
2016:19 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 140
2016:20 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 300
2016:21 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100
2016:22 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100
2016:23 ▇▇▇▇▇▇▇▇▇▇ 50

はい、便利ー

16
13
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
16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?