grepコマンドは、文字列検索や特定の文字列を持つファイルの検索を行うことができます。
例えば、ファイルの中にあるパターンにマッチした行を検索や逆にパターンにマッチしなかった行を表示することが可能です。
目次
- 1 grepとは
- 2 grepコマンドの構文
- 3 コマンドの例に使われるファイルの内容
- 4 パターンにマッチした行を抽出
- 5 大文字と小文字を区別しない(-iオプション)
- 6 複数のパターンを指定(-eオプション)
- 7 複数のパターンをファイルで指定(-fオプション)
- 8 マッチしない行を抽出(-vオプション)
- 9 行がパターンにそのまま一致する行を抽出(-xオプション)
- 10 パターンにマッチした行数を数える(-cオプション)
- 11 パターンにマッチする行数を制限(-mオプション)
- 12 パターンにマッチした部分のみを抜き出す(-oオプション)
- 13 ファイル名を表示(-Hオプション)
- 14 ファイル名を表示しない(-hオプション)
- 15 標準入力に渡されたものにラベルをつける(--labelオプション)
- 16 パターンにマッチした部分の行番号を表示(-nオプション)
- 17 パターンにマッチしているファイルを検索(-lオプション)
- 18 再帰的にファイルに対してパターンにマッチした行を検索(-rオプション)
- 19 再帰的な検索から検索項目を除外(--excludeオプション)(--exclude-fromオプション)(--exclude-dirオプション)
- 20 指定のファイルのみを検索(--includeオプション)
- 21 シンボリックリンクのディレクトリも再帰検索(-Rオプション)
- 22 パターンにマッチした行の後ろのn行を表示(-Aオプション)
- 23 パターンにマッチした行の前からn行を表示(-Bオプション)
- 24 パターンにマッチした行の前後n行を表示(-Cオプション)
- 25 前後に表示した時の行の区切り文字を変更(--group-separator=string)
- 26 前後に表示したときの行の区切り文字を表示しない(--no-group-separator)
- 27 参考
grepとは
文字列検索やパターンにマッチしたファイルの検索を行うことができるコマンドです。
ファイルの文字列検索はマッチした行だけではなく、マッチしなかった行の表示も行うことができます。
パターンにマッチしたファイル名や行番号も表示することができます。また、パターンにマッチした行の前後の行やパターンにマッチした行の行数の表示も行うことができます。
grepコマンドの構文
grepコマンドの構文
1 |
grep オプション パターン 入力ファイル |
オプション:0個以上のオプションをつけることができます。
パターン:'-e パターン' または '-f ファイル名'でパターンを指定する場合、必要ありません。
入力ファイル:0個以上の入力ファイルを指定できます。
コマンドの例に使われるファイルの内容
apple、onion、yuzuの3つの単語を一行ごとに3つ並べたファイルをitemtext.txtとします。行数は27行(=3×3×3)となります。
itemtext.txt
1 2 3 4 5 6 7 8 |
apple apple apple apple apple onion apple apple yuzu apple onion apple apple onion onion apple onion yuzu apple yuzu apple ...(以下省略) |
パターンにマッチした行を抽出
itemtext.txtに書かれているappleのある行がすべて抽出されます。
コマンド例
1 |
grep apple itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 |
apple apple apple apple apple onion apple apple yuzu apple onion apple apple onion onion apple onion yuzu ...(以下省略) |
端末(terminal)によってはcolorのオプションをつけると抽出したパターンが色付けされて、より分かりやすくなります。例えば、
1 |
grep --color=auto apple itemtext.txt |
とするとappleという単語に色がついて、どこにappleという単語があるかがわかりやすくなります。
大文字と小文字を区別しない(-iオプション)
-iオプションは、パターンの大文字と小文字を区別しないようになります。
どのような文字かだけがわかっている場合に利用することができます。
apt-cacheコマンドなどでパッケージを検索するときやシステム情報等を検索するときなどに便利です。
コマンド例では、sedコマンドで行の1番目にあるappleを大文字にしています。
コマンド例
1 |
sed s/apple/APPLE/ itemtext.txt | grep -i apple |
実行結果(一部省略)
1 2 3 4 5 6 7 |
APPLE apple apple APPLE apple onion APPLE apple yuzu APPLE onion apple APPLE onion onion APPLE onion yuzu ...(以下省略) |
複数のパターンを指定(-eオプション)
-eオプションで指定するパターンを明示することができます。
また、-eオプションは複数つけることができ、複数つけると一致したパターンのいずれかが含まれていたら、その行が抽出されます。
コマンド例
1 |
grep -e"apple" -e"yuzu" itemtext.txt |
複数のパターンをファイルで指定(-fオプション)
-fオプションは複数のパターンをファイルによっても指定できます。
パターンの書き方は指定するパターンを一行ずつに分けて書きます。
コマンド例の実行結果は上の-eオプションを複数回使用してappleとyuzuを指定したものと同じになります。
コマンド例
1 |
grep -f pattern.txt itemtext.txt |
pattern.txt
1 2 |
apple yuzu |
マッチしない行を抽出(-vオプション)
-vオプションによって、マッチしない行が抽出されます。
余分なものをフィルタするときによく使われ、パイプなどで次のコマンドに渡す出力を制限するときに便利です。
コマンド例
1 |
grep -v apple itemtext.txt |
実行結果
1 2 3 4 5 6 7 8 |
onion onion onion onion onion yuzu onion yuzu onion onion yuzu yuzu yuzu onion onion yuzu onion yuzu yuzu yuzu onion yuzu yuzu yuzu |
行がパターンにそのまま一致する行を抽出(-xオプション)
-xオプションは、行がパターンにそのまま一致するときにその行を抽出します。パターンの指定で正規表現で^と$でパターンを囲んだもの("^pattern$"のようなもの)と同じになります。
コマンド例
1 |
grep -x apple itemtext.txt |
上のコマンド例の実行結果は出力されません。別の例だと、以下のようになります。
コマンド例
1 |
grep -x apple x_opt.txt |
x_opt.txt
1 2 3 4 5 6 |
apple onion yuzu apple onion yuzu |
実行結果
1 2 |
apple apple |
-xオプションはほかのコマンドと組み合わせて利用できます。例えば、itemtext.txtの3列目にあるappleとyuzuの数を数えるとします。それは、以下のようなコマンドで数えることができます。
1 2 3 4 |
cut -d' ' -f3 itemtext.txt | grep -x -e"apple" -e"yuzu" | sort | uniq -c |
このコマンドの実行結果は
1 2 |
9 apple 9 yuzu |
のようになります。
パターンにマッチした行数を数える(-cオプション)
-cオプションでパターンにマッチした行数を数えることができます。
コマンド例
1 |
grep -c apple itemtext.txt |
実行結果
1 |
19 |
-vオプションを組み合わせると逆にパターンにマッチしなかった行数を数えることができます。
コマンド例
1 |
grep -c -v apple itemtext.txt |
実行結果
1 |
8 |
パターンにマッチする行数を制限(-mオプション)
-mオプションは、マッチした行数が指定した数値まで読み込むと、それ以降のそのファイルに対するパターンマッチングをストップします。
コマンド例
1 |
grep -m 3 apple itemtext.txt |
実行結果
1 2 3 |
apple apple apple apple apple onion apple apple yuzu |
ファイルを複数選択していた場合の結果は、以下の通りになります。ここで、itemtext2.txtはitemtext.txtのコピーになります。
コマンド例
1 |
grep -m 3 apple itemtext.txt itemtext2.txt |
実行結果
1 2 3 4 5 6 |
itemtext.txt:apple apple apple itemtext.txt:apple apple onion itemtext.txt:apple apple yuzu itemtext2.txt:apple apple apple itemtext2.txt:apple apple onion itemtext2.txt:apple apple yuzu |
パターンにマッチした部分のみを抜き出す(-oオプション)
-oオプションは、パターンにマッチした部分のみを抜き出します。出力にはマッチした部分が行に分かれて表示されます。
コマンド例
1 |
grep -o apple itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 |
apple apple apple apple apple ...(以下省略) |
uniqコマンドと組み合わせれば、出てきたパターンの数を数え上げることができます。
実行結果の27は3(1行はappleが3つ)+12(6行はappleが2つ)+12(12行はappleが1つ)でappleの単語が27個出力された結果です。
コマンド例
1 |
grep -o apple itemtext.txt | uniq -c |
実行結果
1 |
27 apple |
複数のパターンを指定している場合、uniqコマンドの前にsortコマンドを通すことで、それぞれのパターンについて、出てきた数を数え上げることができます。
コマンド例
1 |
grep -o -eapple -eyuzu itemtext.txt | sort | uniq -c |
実行結果
1 2 |
27 apple 27 yuzu |
ファイル名を表示(-Hオプション)
-Hオプションはパターンを検索したとき、パターンに一致した行と一緒にファイル名も一緒に表示します。2つ以上のファイルに対して検索するとき、自動的にこのオプションはONになります。
コマンド例
1 |
grep -H apple itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 |
itemtext.txt:apple apple apple itemtext.txt:apple apple onion itemtext.txt:apple apple yuzu itemtext.txt:apple onion apple itemtext.txt:apple onion onion itemtext.txt:apple onion yuzu ...(以下省略) |
ファイル名を表示しない(-hオプション)
-hオプションはパターンに一致した行に対して、ファイル名を非表示にします。
ここで、itemtext2.txtはitemtext.txtのコピーになります。
通常、2つ以上のファイルに対して、grepコマンドを行うとパターンに一致した行と一緒にファイル名も表示されます。しかし、-hオプションをつけることで、ファイル名を非表示にできます。
コマンド例
1 |
grep -h "apple apple apple" itemtext.txt itemtext2.txt |
実行結果
1 2 |
apple apple apple apple apple apple |
標準入力に渡されたものにラベルをつける(--labelオプション)
--labelオプションは、標準入力から来た文字列に対して、ファイル名があるかのようにラベルをつけることができます。
まずは、標準入力でgrepコマンドに-Hオプションをつけて渡された時の出力を見ます。コマンドは
1 |
cat itemtext.txt | grep -H "apple apple apple" |
で、その実行結果は
1 |
(standard input):apple apple apple |
になります。
この(standard input)に対して、--labelというオプションで別の名前を付けることができます。
コマンド例
1 |
cat itemtext.txt | grep -H --label=foo "apple apple apple" |
実行結果
1 |
foo:apple apple apple |
パターンにマッチした部分の行番号を表示(-nオプション)
-nオプションは、パターンに一致した部分の行番号を表示します。
コマンド例
1 |
grep -n apple itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 |
1:apple apple apple 2:apple apple onion 3:apple apple yuzu 4:apple onion apple 5:apple onion onion 6:apple onion yuzu ...(以下省略) |
ファイル名を表示する-Hオプションと併用すると、どのファイルのどの行がパターンに一致したかを把握することができます。
コマンド例
1 |
grep -H -n apple itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 |
itemtext.txt:1:apple apple apple itemtext.txt:2:apple apple onion itemtext.txt:3:apple apple yuzu itemtext.txt:4:apple onion apple itemtext.txt:5:apple onion onion itemtext.txt:6:apple onion yuzu ...(以下省略) |
パターンにマッチしているファイルを検索(-lオプション)
-lオプションは、パターンの後にファイル名を指定することでパターンにマッチした行ではなく、ファイルの名前を表示してくれます。
コマンド例
1 |
grep -l apple dir1 empty.txt itemtext.txt itemtext2.txt |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
.(ドット)はカレントディレクトリ
itemtext2.txtとitemtext-dir1.txtはitemtext.txtのコピー
empty.txtとempty-dir1.txtは何も書かれていないファイル
実行結果
1 2 3 |
grep: dir1: Is a directory itemtext.txt itemtext2.txt |
ディレクトリを指定した場合に違うメッセージを出しますが、-sオプションを用いることでこのようなメッセージやエラーメッセージを表示させないようにすることができます。
コマンド例
1 |
grep -l -s apple * |
実行結果
1 2 |
itemtext.txt itemtext2.txt |
再帰的にパターンにマッチしているファイルを検索(-rオプションと組み合わせ)
-rオプションは、ファイルの代わりにディレクトリを指定して、再帰的にそれぞれのファイルに対して検索を行っていくオプションです。
コマンド例
1 |
grep -l -r apple . |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果
1 2 3 |
./dir1/itemtext-dir1.txt ./itemtext2.txt ./itemtext.txt |
ディレクトリを指定しなければ、現在のディレクトリから再帰的に検索していきます。
コマンド例
1 |
grep -lr apple |
実行結果
1 2 3 |
dir1/itemtext-dir1.txt itemtext2.txt itemtext.txt |
再帰的にファイルに対してパターンにマッチした行を検索(-rオプション)
-rオプションは、ファイルの代わりにディレクトリを指定して、再帰的にそれぞれのファイルに対して検索を行っていくオプションです。
ディレクトリを指定しなければ、現在のディレクトリから再帰的に検索していきます。
コマンド例
1 |
grep -r apple . |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果(以下省略)
1 2 3 4 5 6 7 8 9 10 11 12 |
./dir1/itemtext-dir1.txt:apple apple apple ./dir1/itemtext-dir1.txt:apple apple onion ./dir1/itemtext-dir1.txt:apple apple yuzu ...(中略) ./itemtext2.txt:apple apple apple ./itemtext2.txt:apple apple onion ./itemtext2.txt:apple apple yuzu ...(中略) ./itemtext.txt:apple apple apple ./itemtext.txt:apple apple onion ./itemtext.txt:apple apple yuzu ...(以下省略) |
ディレクトリを複数指定した場合は、入力した順番のディレクトリでファイルを再帰的に実行していきます。
コマンド例
1 |
grep -r apple . dir1/ |
実行結果(以下省略)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
./dir1/itemtext-dir1.txt:apple apple apple ./dir1/itemtext-dir1.txt:apple apple onion ./dir1/itemtext-dir1.txt:apple apple yuzu ...(中略) ./itemtext2.txt:apple apple apple ./itemtext2.txt:apple apple onion ./itemtext2.txt:apple apple yuzu ...(中略) ./itemtext.txt:apple apple apple ./itemtext.txt:apple apple onion ./itemtext.txt:apple apple yuzu ...(中略) dir1/itemtext-dir1.txt:apple apple apple dir1/itemtext-dir1.txt:apple apple onion dir1/itemtext-dir1.txt:apple apple yuzu ...(以下省略) |
再帰的な検索から検索項目を除外
(--excludeオプション)
(--exclude-fromオプション)
(--exclude-dirオプション)
--excludeオプション
--excludeオプションは、検索から除外するファイルを指定します。指定するファイルについてはワイルドカードを使用できます。*.txtですべての.txtファイルを検索から除外します。今回の例では出力は表示されません。
コマンド例
1 |
grep -r --exclude=*.txt apple |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果
なし
'{}'で複数のリストを指定することもできます。
1 |
grep -r --exlude=*.{txt,dat} |
この指定方法は
1 |
grep -r --exclude=*.txt --exclude=*.dat apple |
と等価なコマンドになります。これはすべての.txtファイルとすべての.datファイルを除外することができます。
--exclude-fromオプション
--excludeオプションで除外するファイルは別のファイルにすることができます。
--exclude-fromオプションは、--excludeオプションで除外するファイルを別のファイルに書き込んで置き、そのファイルを読み込んで検索から除外することができます。
コマンド例
1 |
grep -r --exclude-from=exclude-file.txt apple |
exclude-file.txt
1 2 |
*.txt *.dat |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果
なし
--exclude-dirオプション
--exclude-dirオプションは検索から除外できるディレクトリを指定することができます。
ワイルドカードを用いた指定もできます。
コマンド例
1 |
grep -r --exclude-dir=dir1 apple |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 8 |
itemtext2.txt:apple apple apple itemtext2.txt:apple apple onion itemtext2.txt:apple apple yuzu ...(中略) itemtext.txt:apple apple apple itemtext.txt:apple apple onion itemtext.txt:apple apple yuzu ...(以下省略) |
指定のファイルのみを検索(--includeオプション)
--includeオプションは指定したファイルだけを検索します。*.txtですべての.txtファイルだけを検索します。--excludeオプションと同じようにワイルドカードを用いて指定することができます。
コマンド例
1 |
grep -r --include=*.txt apple |
ディレクトリ構造
1 2 3 4 5 6 7 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── empty.txt ├── itemtext.txt └── itemtext2.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 8 9 10 11 12 |
dir1/itemtext-dir1.txt:apple apple apple dir1/itemtext-dir1.txt:apple apple onion dir1/itemtext-dir1.txt:apple apple yuzu ...(中略) itemtext2.txt:apple apple apple itemtext2.txt:apple apple onion itemtext2.txt:apple apple yuzu ...(中略) itemtext.txt:apple apple apple itemtext.txt:apple apple onion itemtext.txt:apple apple yuzu ...(以下省略) |
シンボリックリンクのディレクトリも再帰検索(-Rオプション)
-Rオプションはシンボリックリンクの先のディレクトリやファイルも再帰検索するオプションです。通常の-rオプションではシンボリックリンクの先までは再帰検索を行いません。
-Rオプションで検索の除外をする際、-rオプションと扱いは同じになります。シンボリックリンクの先がディレクトリの場合は--exclude-dirオプションで、シンボリックリンクの先がファイルの場合は--excludeで除外することができます。
コマンド例
1 |
grep -R apple |
ディレクトリ構造
1 2 3 4 5 6 7 8 9 |
. ├── dir1 │ ├── empty-dir1.txt │ └── itemtext-dir1.txt ├── dir1_sym -> /home/ubuntu/test_grep/dir1 ├── empty.txt ├── itemtext.txt ├── itemtext2.txt └── itemtext_sym.txt -> /home/ubuntu/test_grep/itemtext.txt |
実行結果(一部省略)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
dir1/itemtext-dir1.txt:apple apple apple dir1/itemtext-dir1.txt:apple apple onion dir1/itemtext-dir1.txt:apple apple yuzu ...(中略) dir1_sym/itemtext-dir1.txt:apple apple apple dir1_sym/itemtext-dir1.txt:apple apple onion dir1_sym/itemtext-dir1.txt:apple apple yuzu ...(中略) itemtext2.txt:apple apple apple itemtext2.txt:apple apple onion itemtext2.txt:apple apple yuzu ...(中略) itemtext.txt:apple apple apple itemtext.txt:apple apple onion itemtext.txt:apple apple yuzu ...(中略) itemtext_sym.txt:apple apple apple itemtext_sym.txt:apple apple onion itemtext_sym.txt:apple apple yuzu ...(以下省略) |
パターンにマッチした行の後ろのn行を表示(-Aオプション)
-Aオプションはパターンマッチングした行の後ろの行も表示することができます。
何行を表示するかは-Aオプションの後の数字で指定します。
この例では、3を指定したことでパターンマッチした行の後ろ3行分表示しています。
コマンド例
1 |
grep -A 3 "onion onion onion" itemtext.txt |
実行結果
1 2 3 4 |
onion onion onion onion onion yuzu onion yuzu apple onion yuzu onion |
-Aオプションが設定されていても、grepコマンドは表示する行を何度も繰り返しません。
コマンド例
1 |
grep -A 1 "apple" itemtext.txt |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
apple apple apple apple apple onion apple apple yuzu apple onion apple apple onion onion apple onion yuzu apple yuzu apple apple yuzu onion apple yuzu yuzu onion apple apple onion apple onion onion apple yuzu onion onion apple onion onion onion -- onion yuzu apple onion yuzu onion -- yuzu apple apple yuzu apple onion yuzu apple yuzu yuzu onion apple yuzu onion onion -- yuzu yuzu apple yuzu yuzu onion |
パターンにマッチした行の前からn行を表示(-Bオプション)
-Bオプションはパターンマッチングした行の前の行を表示することができます。
何行を表示するかは-Bオプションの後の数字で指定します。
この例では、3を指定したことでパターンマッチした行の前3行分表示しています。
コマンド例
1 |
grep -B 3 "onion onion onion" itemtext.txt |
実行結果
1 2 3 4 |
onion apple onion onion apple yuzu onion onion apple onion onion onion |
-Bオプションが設定されていても、-Aオプションと同様にgrepコマンドは表示する行を何度も繰り返しません。
パターンにマッチした行の前後n行を表示(-Cオプション)
-Cオプションはパターンマッチングした行の前後の行を表示することができます。
何行を表示するかは-Cオプションの後の数字で指定します。
この例では、3を指定したことでパターンマッチした行の前後3行分表示しています。
コマンド例
1 |
grep -C 3 "onion onion onion" itemtext.txt |
実行結果
1 2 3 4 5 6 7 |
onion apple onion onion apple yuzu onion onion apple onion onion onion onion onion yuzu onion yuzu apple onion yuzu onion |
-Cオプションが設定されていても、-Aオプションと同様にgrepコマンドは表示する行を何度も繰り返しません。
前後に表示した時の行の区切り文字を変更(--group-separator=string)
--group-separatorオプションは、-A, -B, -Cオプションの時に、パターンマッチした行の前後の表示で使われていた区切り文字を任意の文字列で変更することができます。
コマンド例
1 |
grep -A 1 --group-separator="*****separator*****" "apple" itemtext.txt |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
apple apple apple apple apple onion apple apple yuzu apple onion apple apple onion onion apple onion yuzu apple yuzu apple apple yuzu onion apple yuzu yuzu onion apple apple onion apple onion onion apple yuzu onion onion apple onion onion onion *****separator***** onion yuzu apple onion yuzu onion *****separator***** yuzu apple apple yuzu apple onion yuzu apple yuzu yuzu onion apple yuzu onion onion *****separator***** yuzu yuzu apple yuzu yuzu onion |
前後に表示したときの行の区切り文字を表示しない(--no-group-separator)
--no-group-separatorオプションは、-A,-B,-Cオプションの時に、パターンマッチした行の前後の表示で使われていた区切り文字を表示しないようにすることができます。
コマンド例
1 |
grep -A 1 --no-group-separator "apple" itemtext.txt |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
apple apple apple apple apple onion apple apple yuzu apple onion apple apple onion onion apple onion yuzu apple yuzu apple apple yuzu onion apple yuzu yuzu onion apple apple onion apple onion onion apple yuzu onion onion apple onion onion onion onion yuzu apple onion yuzu onion yuzu apple apple yuzu apple onion yuzu apple yuzu yuzu onion apple yuzu onion onion yuzu yuzu apple yuzu yuzu onion |