1
1

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 5 years have passed since last update.

GDB: Grep De ばーっと調べる

Posted at

なにをするのか

Linux で、たくさんのコードからいろいろ検索したいときのシェルスクリプトワンライナー

具体例

src ディレクトリ以下の、拡張子 .c のファイルから、iterator を含む行を探す

find で探して、grep で検索する。 -H オプションをつけるとファイル名が出てくる。

find src -name "*.c" -exec grep -H iterator {} \;

src ディレクトリ以下の、拡張子 .c のファイルから、iterator を含む行を探す。ただし、int がある行はいらない

grep -v でいらない部分を捨てる

find src -name "*.c" -exec grep -H iterator {} \; | grep -v int

src ディレクトリ以下の、拡張子 .c のファイルから、iterator を含む行を探す。ただし、その行の前2行と後3行もみたい。

-A で後、-B で前の行を表示する

find src -name "*.c" -exec grep -H -B 2 -A 3 iterator {} \;

src ディレクトリ以下の、拡張子 .c のファイルから、iterator を含むファイル名だけが知りたい

ファイル名と行は : で区切られているので、冒頭だけ取ってきて重複をのぞく。

find src -name "*.c" -exec grep -H iterator {}  \; | cut -d":" -f 1 | sort | uniq
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?