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?

More than 3 years have passed since last update.

ソースコードの行数を調べてヒストグラムを作成するワンライナー

Posted at

準備

bitly/data_hacks を使うと簡単にヒストグラムが出力できる。

それをあらかじめインストールしておく(※Python2.x系でしか動かないっぽい)

pip install data_hacks

ワンライナー

以下のワンライナーでデータを収集してヒストグラムの出力ができる。

find ./app -name *.rb |xargs wc -l | sed '$d' | awk '{ print $1 }' | histogram.py 

出力結果

# NumSamples = 1359; Min = 1.00; Max = 746.00
# Mean = 61.732156; Variance = 6981.429363; SD = 83.554948; Median 34.000000
# each ∎ represents a count of 13
    1.0000 -    75.5000 [  1047]: ∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
   75.5000 -   150.0000 [   183]: ∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  150.0000 -   224.5000 [    65]: ∎∎∎∎∎
  224.5000 -   299.0000 [    34]: ∎∎
  299.0000 -   373.5000 [     9]:
  373.5000 -   448.0000 [     6]:
  448.0000 -   522.5000 [     6]:
  522.5000 -   597.0000 [     6]:
  597.0000 -   671.5000 [     2]:
  671.5000 -   746.0000 [     1]:

やってること

個々のコマンドでやっていること書いておく

ファイルを抽出

vendor/bundle とか含めると邪魔なので ./app 配下の *.rb ファイルだけに絞り込んでいる。

find ./app -name *.rb

ファイルの行数を調べる

find の結果、出力された個々のファイルに対して wc コマンドを実行して行数を求める

xargs wc -l

最終行(total)を取り除く

sed コマンドで最終行を取り除く

sed '$d'

行数の部分を取り除く

上記までだと、行ごとに 175 ./app/hoge/fuga.rb のような出力になっているので、これの行数を表す数字部分だけを抽出する。

awk '{ print $1 }'

ヒストグラムを作成する

histogram.py

できあがり

さいごに

sedawk の使い方、いつもググって出てきたのを参考にしているので、もっとちゃんとした使い方がありそう。

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?