LoginSignup
3
3

More than 5 years have passed since last update.

頭の悪い grep の使い方

Last updated at Posted at 2015-10-13

特定ディレクトリ以下のHTML(的な)ファイルで、使われているCSSクラスを列挙しようとして書いた。
最適化はできると思うんだけど、これで求めたいものがとれたのでよしとする。

find [directory] -type f -name [filepattern] \
| xargs grep -o "[^-]class=\"[^\"]*\"" \
| grep -o "\"\(.*\)\"" \
| grep -o "[^\"].*[^\"]" \
| sed -e 's/\s\+/\n/g' \
| sort \
| uniq

特記事項として、2行目で[^-]classとすることで、ng-classが引っかかるのを防いだくらい。

grep -o 三段重ねとかほんと頭悪い。うーあー。

追記

↑の出力を[dictionary]ファイルとして、[css]ファイル中にいくつ出現するか?

for line in `cat [dictionary]`; do M=`grep -c "\.$line" [css]`; if [ $M -ne 0 ];then echo $line: $M; fi;done;

複数のCSSフレームワークがぶっこまれたプロジェクトで、片方とさよならしたくて調査する・・・そのためだけにこんな半端なシェル芸をすることに・・・(´・ω・`)

3
3
3

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
3
3