どうも、シローです。
今回は、コマンドで文字列を扱う際によく使われるgrep
コマンドについてまとめようと思います。
grepコマンドとは
grep
コマンドは標準入力から渡された文字列に特定の文字列が含まれるか検索することができます。
grepコマンドの書式
1 |
grep [オプション] 検索パターン 入力ファイル1 入力ファイル2 |
grepコマンドの使用例
こちらのファイルから検索パターンにマッチした文字列を出力します。
grep_sample.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 27 28 29 |
test1/file_1 test1/file_2 test1/file_3 test2/file_1 test2/file_2 test2/file_3 test3/file_1 test3/file_2 test3/file_3 test4/file_1 test4/file_2 test4/file_3 test5/file_1 test5/file_2 test5/file_3 test6/file_1 test6/file_2 test6/file_3 test7/file_1 test7/file_2 test7/file_3 test8/file_1 test8/file_2 test8/file_3 test9/file_1 test9/file_2 test9/file_3 work/test1/file_x work/test5/file_1 |
実行
1 2 3 4 5 |
$ grep 'test1' grep_sample.txt test1/file_1 test1/file_2 test1/file_3 work/test1/file_x |
検索パターンには正規表現が使用できる
文字列の検索パターンには正規表現が使用できます。
文字にマッチするメタ文字
基本正規表現 | 拡張正規表現 | 意味 |
. | . | 任意の1文字 |
[ ] | [ ] | []の中に含まれるいずれかの1文字 |
[^ ] | [ ] | []の中に含まれないいずれかの1文字 |
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 27 28 29 30 |
$ grep 'test9.' grep_sample.txt test9/file_1 test9/file_2 test9/file_3 $ grep 'test[123]' grep_sample.txt test1/file_1 test1/file_2 test1/file_3 test2/file_1 test2/file_2 test2/file_3 test3/file_1 test3/file_2 test3/file_3 work/test1/file_x $ grep 'test[^12345]' grep_sample.txt test6/file_1 test6/file_2 test6/file_3 test7/file_1 test7/file_2 test7/file_3 test8/file_1 test8/file_2 test8/file_3 test9/file_1 test9/file_2 test9/file_3 |
位置にマッチするメタ文字
基本正規表現 | 拡張正規表現 | 意味 |
^ | ^ | 行頭 |
$ | $ | 行末 |
1 2 3 4 5 6 7 |
$ grep '^test5' grep_sample.txt test5/file_1 test5/file_2 test5/file_3 $ grep '^test5/file_1$' grep_sample.txt test5/file_1 |
繰り返しを指定するメタ文字
基本正規表現 | 拡張正規表現 | 意味 |
* | * | 直前の文字の0回以上の繰り返し |
なし | + | 直前の文字の1回以上の繰り返し |
なし | ? | 直前の文字の0回または1回の繰り返し |
\{m,n\} | {m,n} | 直前の文字のm回以上n回以下の繰り返し |
\{m\} | {m} | 直前の文字のちょうどm回の繰り返し |
\{m,\} | {m,} | 直前の文字のm回以上の繰り返し |
grep_sample2.txt
1 2 3 4 |
test1 test111 test111111 test123456 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ grep -E 'test11*' grep_sample2.txt test1 test111 test111111 test123456 $ grep -E 'test1+' grep_sample2.txt test1 test111 test111111 test123456 $ grep -E 'test1{1}$' grep_sample2.txt test1 $ grep -E 'test1{1,3}$' grep_sample2.txt test1 test111 $ grep -E 'test.{6}$' grep_sample2.txt test111111 test123456 |
その他のメタ文字
基本正規表現 | 拡張正規表現 | |
\ | \ | 直後のメタ文字の意味を打ち消す |
\( \) | ( ) | グループ化する |
なし | | | 複数の正規表現をOR条件で連結する |
grep_sample3.txt
1 2 3 4 5 |
aaa abcbc abbbccc abcxxxbc axxxxxx |
1 2 3 4 5 6 7 8 9 10 |
$ grep -E 'a(bc){2}' grep_sample3.txt abcbc $ grep -E 'a(bc|xxx){2}' grep_sample3.txt abcbc abcxxxbc axxxxxx $ grep -E 'a(bc|xxx){3}' grep_sample3.txt abcxxxbc |
grepコマンドのオプション
grep
コマンドには検索条件を指定するオプションと出力内容を指定するオプションがあります。
検索パターンを指定するオプション
オプション | 内容 |
-E | 拡張正規表現を使用する |
-F | 正規表現を使用しない |
-i | 大文字と小文字を区別しない |
-e 検索パターン | 検索パターンを指定する |
-v | 検索結果を反転させる |
よく使うのは、E
,e
,v
あたりでしょうか。
出力内容を指定するオプション
オプション | 内容 |
-n | 行番号を出力に含める |
-H | マッチしたファイルのファイル名を出力に含める |
-h | マッチしたファイルのファイル名を出力に含めない |
-l | マッチしたファイルのファイル名だけを出力に含める |
-L | マッチしていないファイルのファイル名だけを出力に含める |
-o | 行全体ではなく検索パターンにマッチした箇所だけを出力する |
-q | マッチした結果を出力しない |
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 27 28 29 30 31 32 33 |
$ grep 'test1' grep_sample.txt grep_sample2.txt grep_sample.txt:test1/file_1 grep_sample.txt:test1/file_2 grep_sample.txt:test1/file_3 grep_sample.txt:work/test1/file_x grep_sample2.txt:test1 grep_sample2.txt:test111 grep_sample2.txt:test111111 grep_sample2.txt:test123456 $ grep -n 'test1' grep_sample.txt grep_sample2.txt grep_sample.txt:1:test1/file_1 grep_sample.txt:2:test1/file_2 grep_sample.txt:3:test1/file_3 grep_sample.txt:28:work/test1/file_x grep_sample2.txt:1:test1 grep_sample2.txt:2:test111 grep_sample2.txt:3:test111111 grep_sample2.txt:4:test123456 $ grep -l 'test1' grep_sample.txt grep_sample2.txt grep_sample.txt grep_sample2.txt $ grep -o 'test1' grep_sample.txt grep_sample2.txt grep_sample.txt:test1 grep_sample.txt:test1 grep_sample.txt:test1 grep_sample.txt:test1 grep_sample2.txt:test1 grep_sample2.txt:test1 grep_sample2.txt:test1 grep_sample2.txt:test1 |
まとめ
grep
は文字列を検索するコマンド- 検索文字パターンには正規表現を使用できる
- オプションには検索パターンを指定するものと出力内容を指定するものがある
- 拡張正規表現を使用する時には
-E
オプションを使用
- 拡張正規表現を使用する時には