2025-02-07 15:15:51 +09:00
|
|
|
# grep
|
|
|
|
|
|
|
|
> 正規表現を使ってファイルのパターンを見つけます。
|
|
|
|
> もっと詳しく: <https://www.gnu.org/software/grep/manual/grep.html>。
|
|
|
|
|
|
|
|
- ファイル内のパターンを検索する:
|
|
|
|
|
|
|
|
`grep "{{検索パターン}}" {{path/to/file}}`
|
|
|
|
|
|
|
|
- 正確な文字列を検索する(正規表現を無効にする):
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`grep {{[-F|--fixed-strings]}} "{{正確な文字列}}" {{path/to/file}}`
|
2025-02-07 15:15:51 +09:00
|
|
|
|
|
|
|
- ディレクトリ内の全てのファイルを再帰的にパターン検索し、マッチしたファイルの行番号を表示する:
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`grep {{[-r|--recursive]}} {{[-n|--line-number]}} --binary-files {{without-match}} "{{検索パターン}}" {{path/to/directory}}`
|
2025-02-07 15:15:51 +09:00
|
|
|
|
|
|
|
- 拡張正規表現 (`?`, `+`, `{}`, `()`, `|` をサポート)を大文字小文字を区別しないモードで使用する:
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`grep {{[-E|--extended-regexp]}} {{[-i|--ignore-case]}} "{{検索パターン}}" {{path/to/file}}`
|
2025-02-07 15:15:51 +09:00
|
|
|
|
|
|
|
- 各マッチの前後3行のコンテキストを表示する:
|
|
|
|
|
|
|
|
`grep --{{context|before-context|after-context}} 3 "{{検索パターン}}" {{path/to/file}}`
|
|
|
|
|
|
|
|
- 各マッチのファイル名と行番号をカラー出力する:
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`grep {{[-H|--with-filename]}} {{[-n|--line-number]}} --color=always "{{検索パターン}}" {{path/to/file}}`
|
2025-02-07 15:15:51 +09:00
|
|
|
|
|
|
|
- パターンにマッチする行を検索し、マッチしたテキストのみを表示する:
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`grep {{[-o|--only-matching]}} "{{検索パターン}}" {{path/to/file}}`
|
2025-02-07 15:15:51 +09:00
|
|
|
|
|
|
|
- パターンにマッチしない行を `stdin` から検索する:
|
|
|
|
|
2025-03-14 09:18:28 +02:00
|
|
|
`cat {{path/to/file}} | grep {{[-v|--invert-match]}} "{{検索パターン}}"`
|