はじめに

グラフヒストグラムを作成してくれる distributionコマンドがあるとのことで使ってみる。

環境

1
2
Mac OS Sonoma 14.4.1
Homebrew 4.2.20

distributionのインストール

https://formulae.brew.sh/formula/distribution

1
brew install distribution
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
brew install distribution
==> Downloading https://ghcr.io/v2/homebrew/core/distribution/manifests/1.3
################################################################################################################################################# 100.0%
==> Fetching distribution
==> Downloading https://ghcr.io/v2/homebrew/core/distribution/blobs/sha256:46e6afa7ee3cdc08f4fde478e6235b1df80813391abe507505e1452926d5aff2
################################################################################################################################################# 100.0%
==> Pouring distribution--1.3.all.bottle.tar.gz
🍺  /usr/local/Cellar/distribution/1.3: 7 files, 119KB
==> Running `brew cleanup distribution`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

distributionを使う

1
2
ls -al | distribution
env: python: No such file or directory

pythonコマンドが必要なようだが、python3になってからこのコマンドはないので、シンボリックリンクを作成した。

1
2
cd /usr/local/bin
ln -s python3 python

実行してみると、python3のWarningが出てる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ls | distribution
/usr/local/bin/distribution:517: SyntaxWarning: invalid escape sequence '\$'
  print("        word     ^[A-Z,a-z]+\$ - tokens/lines must be entirely alphabetic")
/usr/local/bin/distribution:518: SyntaxWarning: invalid escape sequence '\$'
  print("        num      ^\\d+\$        - tokens/lines must be entirely numeric")
/usr/local/bin/distribution:542: SyntaxWarning: invalid escape sequence '\$'
  print("  zcat /var/log/syslog*gz | awk '{print \$5}'  | %s -t=word -m-word -h=15 -c=/" % (scriptName))
/usr/local/bin/distribution:545: SyntaxWarning: invalid escape sequence '\$'
  print("  cat /usr/share/dict/words | awk '{print length(\$1)}' | %s -c=* -w=50 -h=10 | sort -n" % (scriptName))
         Key|Ct (Pct)   Histogram
      themes|1 (7.14%) ---------------------------------------------------------
      static|1 (7.14%) ---------------------------------------------------------
   resources|1 (7.14%) ---------------------------------------------------------
      public|1 (7.14%) ---------------------------------------------------------
netlify.toml|1 (7.14%) ---------------------------------------------------------
     layouts|1 (7.14%) ---------------------------------------------------------
        data|1 (7.14%) ---------------------------------------------------------
     content|1 (7.14%) ---------------------------------------------------------
      config|1 (7.14%) ---------------------------------------------------------
 compose.yml|1 (7.14%) ---------------------------------------------------------
  archetypes|1 (7.14%) ---------------------------------------------------------
   README.md|1 (7.14%) ---------------------------------------------------------
    Makefile|1 (7.14%) ---------------------------------------------------------
      Docker|1 (7.14%) ---------------------------------------------------------

分布は表示される。

プロセスの出力からコマンド部分だけを取得し単語分析を行う

1
2
3
ps axww \
    | cut -c 28- \
    | distribution --tokenize=word --match=word --char='|' --width=90 --height=25
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
              Key|Ct  (Pct)   Histogram
       annotation|872 (6.53%) ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
         Contents|439 (3.29%) ||||||||||||||||||||||||||||||
          Library|391 (2.93%) |||||||||||||||||||||||||||
        framework|381 (2.85%) ||||||||||||||||||||||||||
               on|370 (2.77%) ||||||||||||||||||||||||||
           System|367 (2.75%) |||||||||||||||||||||||||
           Google|349 (2.61%) ||||||||||||||||||||||||
            MacOS|305 (2.28%) |||||||||||||||||||||
           Chrome|298 (2.23%) |||||||||||||||||||||
              app|294 (2.20%) ||||||||||||||||||||
           enable|226 (1.69%) ||||||||||||||||
           client|218 (1.63%) |||||||||||||||
         Versions|214 (1.60%) |||||||||||||||
       Frameworks|211 (1.58%) |||||||||||||||
              usr|197 (1.48%) ||||||||||||||
           Helper|188 (1.41%) |||||||||||||
PrivateFrameworks|182 (1.36%) |||||||||||||
              off|165 (1.24%) ||||||||||||
          libexec|153 (1.15%) |||||||||||
              com|153 (1.15%) |||||||||||
         Renderer|142 (1.06%) ||||||||||
     Applications|142 (1.06%) ||||||||||
               id|140 (1.05%) ||||||||||
                A|137 (1.03%) ||||||||||
         renderer|135 (1.01%) ||||||||||

参考

おわりに

コマンドで単語分析をしてログ解析をするなどの場合に、グラフ化するとかなり便利そうだなーと思った。
単語分析のコマンドというのも面白いので、この辺り勉強して業務に活かしたい。