LoginSignup
12
12

More than 5 years have passed since last update.

grepぷりん :: grepだけで複雑な検索を

Last updated at Posted at 2014-06-17

What is this?

最近マイブームの "grep -Prin" について・・・

  • あるディレクトリ以下(サブディレクトリを含む)の *.c *.h *.s から特定の文字列を検索したい。
  • 報告書を短くまとめたいので、コマンド文字列も短くしたい。
  • 特定の文字列とは実のところアセンブラニモニックなのでキャピタル(大文字小文字)を区別せず検索したい。
  • 後方不一致条件とか付加したいので、PerlやTclの正規表現と等価のものを使いたい。

・・・上記は秀丸エディタからならできるが、自動化したいのでコマンドラインから呼び出せるものにしたい・・・

調べた結果、
grep -Prin --include='*.c' --include='*.s' --include='*.h' <Reg-exp> <TopDir>
で目的達成したのでメモ。(Cygwin32/ubuntu)

Grep Options

-P :: Perl-regexp

このオプションで、パターン指定文字列にPerlの正規表現拡張構文を使用可能となります。
具体的には

(?<![a-zA-Z0-9"_-])

のような後方一致系表記が使用可能になります。

-r :: Recursive

これは有名?
サブディレクトリも検索するオプションです。

-i :: Ignore-case

大文字小文字の区別をしなくなります。

-n :: line-Number

パターン一致した行番号を表示するようになります。

--include=

GLOBと一致したファイル名をもつファイル内から検索します。
man page には明記されていませんでしたが、このオプションを複数指定することで重ねがけできました。

Examples

MIPS32: LD, SD 命令をインライン使用している箇所を検索する

$ grep -Prin --include='*.c' --include='*.s' --include='*.h' '(?<![a-zA-Z0-9"()_-])[LS]D(?![A-Z0-9"()_-])' ./
./alchemy/devboards/db1200.c:329:/* SD carddetects:  they're supposed to be edge-triggered, but ack
./boot/elf2ecoff.c:140:         fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name,
./boot/elf2ecoff.c:568:                                         "Intersegment gap (%ld bytes) too large.\n",
./boot/elf2ecoff.c:573:                                 "Warning: %ld byte intersegment gap.\n",
./include/asm/asm.h:244:#define REG_S           sd
./include/asm/asm.h:245:#define REG_L           ld
     <<<<<途中省略>>>>>

MIPS32: Branch と Jump 命令の検索

$ grep -Prin --include='*.s' '(?<=\s)(B\C|BEQ|BGE|BGT|BLE|BLT|BNE|J\C|JAL|JR)' ./
12
12
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
12
12