LoginSignup
8

More than 1 year has passed since last update.

コマンドライン上でヒストグラム値(頻度数)を求める

Last updated at Posted at 2016-02-05

こんにちは。
コマンドライン上でヒストグラム値(頻度数)を求めるシェルスクリプト を作りました1

下記の動作例は history コマンドの第2カラムを対象としています(すなわち過去に使用したコマンド名)。

$ history | awk '{print $2}' | ./hist.sh | ./hist_graph.sh
            brew   87 ##################################################
             pip   33 ###################
              ls   30 #################
               :
               :

ソース

頻度数を求める処理
hist.sh
#!/bin/sh
cat "$@" | sort | uniq -c | sort -k1nr | awk '{printf "%16s %4d\n",$2,$1;}'
exit 0
図示(#の連続)を付け加える処理
hist_graph.sh
#!/bin/sh
cat "$@" | awk '!max{max=$2;}{f=50/max;if(f>1)f=1;i=$2*f;r="";while(i-->0)r=r"#";printf "%s %s\n",$0,r;}'
exit 0

他の方法

なお改めてヒストグラム実現方法を調べてみると、"in-terminal (ASCII) histograms tool" (Stack Overflow) の中で、下記も紹介されていました。
* https://github.com/bitly/data_hacks
* https://github.com/philovivero/distribution


  1. ただしこの話題は「CSVデータのヒストグラム値(頻度数)の計算(qコマンド利用)」とほとんど同じなので恐縮です。 

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
8