文字列を検索する

複数のファイルからある文字列を探す

一つか二つのファイルからある文字列を探すのは、viのサーチ(検索)の機能を使ってもできるが、たくさんのファイルの中から探すためには grepコマンドを使う方が便利である。

grepの使用例

grepは文字列が見付からなければ、何も表示しない。

# grep test tmp.c      <- tmp.cからtestを探す
test();                <- testという文字列がtmp.cの中にあれば表示する
# grep test *.c
test.c:int test;       <- testという文字列がtest.cの中にある
tmp.c:test();          <- testという文字列がtmp.cの中にある
# grep -l test *.c       <- testという文字列含むファイル名だけを表示する
test.c
tmp.c
# grep -i test tmp.c   <- -iをつけると大文字・小文字を区別しなくなる
int TEST;
float Test;
test();
# grep "int x" tmp.c   <- スペース・タブなどを含む文字列の場合は"で囲む
int x, y;
# grep test *.c | grep int   <- testとintのふたつとも含む行を表示する
test.c:int test;
#

練習課題

まとめ

参考


Prev | Next
Home | Contents
abe@injapan.net