クリップボードにコピーしました!
文字列のgrep

grepで複数パターン検索を使いこなす基本を徹底解説

updated: 2026/05/21 created: 2026/05/21

はじめに

grepはLinuxやmacOSで文字列検索を行う代表的なコマンドです。

特に複数のパターンを扱えるようになると、ログ解析や設定ファイル確認を効率化できます。

本記事ではgrepの複数パターン検索を基礎から実践まで分かりやすく解説します。

参考: GNU grep

grepで複数パターンを使った基本検索の書き方

ファイル作成

cat << 'EOF' > input.txt apple orange banana grape apple melon banana kiwi orange melon grape lemon EOF

実行コマンド

grep -E 'apple|banana' input.txt

実行結果

apple orange banana
grape apple melon
banana kiwi orange

実行コマンド

grep -e 'orange' -e 'grape' input.txt

実行結果

apple orange banana
grape apple melon
banana kiwi orange
melon grape lemon

仕組み

書き方 仕組み
grep -E 'A|B' |で複数パターンをOR検索 grep -E 'apple|banana' input.txt
grep -e pattern1 -e pattern2 -eを複数指定して検索 grep -e 'orange' -e 'grape' input.txt

解説

grepでは複数条件をOR検索として扱えます。
-Eによる正規表現と、-eの複数指定が基本的な使い方です。

grepの複数パターンで複数文字列をOR検索する方法

ファイル作成

cat << 'EOF' > input.txt apple banana orange grape pineapple EOF

実行コマンド

grep -E 'apple|orange' input.txt

実行結果

apple
orange
pineapple

実行コマンド

grep -e 'banana' -e 'grape' input.txt

実行結果

banana
grape

仕組み

方法 コマンド例 仕組み
-E を使う grep -E 'apple|orange' input.txt | を使ってOR条件を指定する
-e を複数使う grep -e 'banana' -e 'grape' input.txt -e ごとに検索条件を追加し、いずれか一致した行を表示

解説

grep では -E と | を使うことで簡潔にOR検索できます。
また、-e を複数指定すると可読性の高い複数条件検索が可能です。

grepの複数パターンで-Eオプションとegrepを使い分ける方法

ファイル作成

cat << 'EOF' > input.txt error: disk full warning: cpu high info: backup completed error: memory leak warning: network unstable EOF

実行コマンド

grep -E 'error|warning' input.txt

実行結果

error: disk full
warning: cpu high
error: memory leak
warning: network unstable

実行コマンド

egrep 'error|warning' input.txt

実行結果

error: disk full
warning: cpu high
error: memory leak
warning: network unstable

仕組み

方法 内容 特徴
grep -E 拡張正規表現を有効化 現在はこちらが推奨
egrep grep -E の省略形 古い環境向けで非推奨の場合あり
error|warning OR条件で複数パターン検索

解説

grep -E は拡張正規表現を利用して複数パターンを簡潔に検索できます。
egrep は同等機能ですが、最近は grep -E の利用が一般的です。

grepの複数パターンで完全一致検索を行う方法

ファイル作成

cat << 'EOF' > input.txt apple banana orange grape melon EOF

ファイル作成

cat << 'EOF' > patterns.txt apple orange melon EOF

実行コマンド

grep -x -E 'apple|orange|melon' input.txt

実行結果

apple
orange
melon

実行コマンド

grep -x -f patterns.txt input.txt

実行結果

apple
orange
melon

仕組み

オプション 内容
-x 行全体を完全一致で検索
-E 拡張正規表現を使用
apple|orange|melon 複数パターンをOR条件で指定
-f patterns.txt パターンをファイルから読み込み

解説

grep -x を使うことで部分一致ではなく完全一致検索を行えます。

検索パターンが多い場合は patterns.txt を利用することで管理しやすくなります。

grepの複数パターンで単語単位のみ一致させる方法

ファイル作成

cat << 'EOF' > input.txt apple pineapple banana orange apple pie banana split grape EOF

実行コマンド

grep -Ew 'apple|banana' input.txt

実行結果

apple
banana
apple pie
banana split

実行コマンド

grep -E 'apple|banana' input.txt

実行結果

apple
pineapple
banana
apple pie
banana split

仕組み

オプション 内容 動作
-E 拡張正規表現を使用 apple|banana のようなOR条件を利用可能
-w 単語単位で一致 前後を単語境界として判定
apple|banana 複数パターン指定 apple または banana に一致

解説

grep -Ew を使うことで、複数パターンをOR条件で検索しつつ、単語単位のみ一致できます。
pineapple のような部分一致を除外したい場合に便利です。

grepの複数パターンで行頭・行末を指定して検索する方法

ファイル作成

cat << 'EOF' > input.txt ERROR: failed to connect INFO: retry started ERROR: timeout detected DEBUG: cache cleared INFO: process completed WARNING: disk usage high EOF

実行コマンド

grep -E '^(ERROR|INFO)|high$' input.txt

実行結果

ERROR: failed to connect
INFO: retry started
ERROR: timeout detected
INFO: process completed
WARNING: disk usage high

仕組み

項目 内容
-E 拡張正規表現を有効化
^ 行頭に一致
$ 行末に一致
(ERROR|INFO) 複数パターンをOR条件で指定
high$ highで終わる行を検索

解説

grep -E を使うことで、複数パターンを簡潔に指定できます。
^ と $ を組み合わせることで、行頭・行末を条件にした柔軟な検索が可能です。

grepの複数パターンで大文字小文字を区別せず検索する方法

ファイル作成

cat << 'EOF' > input.txt Hello World hello grep HELLO LINUX Error Message warning message EOF

実行コマンド

grep -Ei 'hello|error' input.txt

実行結果

Hello World
hello grep
HELLO LINUX
Error Message

仕組み

項目 内容
grep テキスト検索を行うコマンド
-E 拡張正規表現を有効化し | なしで OR 条件を使える
-i 大文字小文字を区別せず検索する
hello|error hello または error に一致する複数パターン
input.txt 検索対象ファイル

解説

-E を使うことで複数パターンをシンプルに記述できます。
-i を組み合わせると、大文字・小文字を区別せず柔軟に検索可能です。

grepの複数パターンでAND検索を行う方法

ファイル作成

cat << 'EOF' > input.txt apple orange banana apple grape orange lemon apple orange banana grape EOF

実行コマンド

grep 'apple' input.txt | grep 'orange'

実行結果

apple orange banana
apple orange

仕組み

仕組み 内容
1つ目のgrep apple を含む行を抽出
パイプ | 前のコマンドの結果を次のコマンドへ渡す
2つ目のgrep orange を含む行だけをさらに抽出
AND検索 両方の条件に一致した行のみ表示

解説

grep はパイプで連結することで複数条件のAND検索を実現できます。
複数キーワードを段階的に絞り込めるため、ログ解析などでよく使われます。

grepの複数パターンでNOT検索を組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt error: database connection failed info: retry connection warning: disk usage high error: timeout occurred debug: cache initialized fatal: unexpected shutdown EOF

実行コマンド

grep -Ev 'error|warning' input.txt

実行結果

info: retry connection
debug: cache initialized
fatal: unexpected shutdown

実行コマンド

grep -v 'error' input.txt | grep -v 'warning'

実行結果

info: retry connection
debug: cache initialized
fatal: unexpected shutdown

仕組み

仕組み コマンド例 内容
複数NOT条件を1回で指定 grep -Ev 'error|warning' input.txt 拡張正規表現を使い、error または warning を含む行を除外
NOT条件を連結 grep -v 'error' | grep -v 'warning' grep -v をパイプで連結し、段階的に除外

解説

grep -v はNOT検索を行うオプションです。
複数条件を扱う場合は -E と | を組み合わせる方法がシンプルで実用的です。

grepの複数パターンで-fオプションからパターンファイルを読み込む方法

ファイル作成

cat << 'EOF' > patterns.txt error warning failed EOF

ファイル作成

cat << 'EOF' > input.txt [INFO] process started [WARNING] disk usage high [ERROR] connection failed [SUCCESS] backup completed failed to connect database EOF

実行コマンド

grep -i -f patterns.txt input.txt

実行結果

[WARNING] disk usage high
[ERROR] connection failed
failed to connect database

仕組み

項目 内容
-f patterns.txt 検索パターンをファイルから読み込む
-i 大文字・小文字を区別しない
patterns.txt 複数の検索キーワードを1行ずつ定義
input.txt 検索対象ファイル
動作 patterns.txt 内のいずれかに一致した行を抽出

解説

grep -f を使うと、複数パターンを管理しやすくなり、大量のキーワード検索にも便利です。
パターンを外部ファイル化することで、保守性や再利用性も向上します。

grepの複数パターンで複数ファイルを同時検索する方法

ファイル作成

cat << 'EOF' > sample1.txt ERROR: Disk full INFO: Service started WARNING: CPU usage high ERROR: Connection timeout EOF

ファイル作成

cat << 'EOF' > sample2.txt INFO: Backup completed WARNING: Memory usage high ERROR: Failed login attempt INFO: System shutdown EOF

実行コマンド

grep -E 'ERROR|WARNING' sample1.txt sample2.txt

実行結果

sample1.txt:ERROR: Disk full
sample1.txt:WARNING: CPU usage high
sample1.txt:ERROR: Connection timeout
sample2.txt:WARNING: Memory usage high
sample2.txt:ERROR: Failed login attempt

実行コマンド

grep -e 'ERROR' -e 'WARNING' sample1.txt sample2.txt

実行結果

sample1.txt:ERROR: Disk full
sample1.txt:WARNING: CPU usage high
sample1.txt:ERROR: Connection timeout
sample2.txt:WARNING: Memory usage high
sample2.txt:ERROR: Failed login attempt

仕組み

方法 内容 特徴
grep -E 'ERROR|WARNING' 拡張正規表現で複数パターンを指定 | の代わりに \
grep -e 'ERROR' -e 'WARNING' -e を複数指定 パターン追加がしやすい
複数ファイル指定 sample1.txt sample2.txt 同時検索できる
出力形式 ファイル名:一致行 どのファイルで一致したか分かる

解説

grep は複数パターン検索と複数ファイル検索を同時に実行できます。
ログ解析やエラー調査で非常に便利な使い方です。

grepの複数パターンでディレクトリを再帰検索する方法

ファイル作成

mkdir -p project/src

ファイル作成

cat << 'EOF' > project/app.log INFO: Application started ERROR: Database connection failed WARN: Retry processing EOF

ファイル作成

cat << 'EOF' > project/error.log FATAL: System crash ERROR: Memory leak detected EOF

ファイル作成

cat << 'EOF' > project/src/main.txt INFO: User login success DEBUG: Session created EOF

ファイル作成

cat << 'EOF' > project/src/debug.txt TRACE: API request WARN: Slow response detected EOF

実行コマンド

grep -r -E "ERROR|WARN" project

実行結果

project/src/debug.txt:WARN: Slow response detected
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected

実行コマンド

grep -r -e "ERROR" -e "WARN" project

実行結果

project/src/debug.txt:WARN: Slow response detected
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected

仕組み

オプション 役割
grep テキスト検索コマンド
-r ディレクトリを再帰検索
-E 拡張正規表現を有効化
"ERROR|WARN" OR条件で複数パターン検索
-e 検索パターンを複数指定
project 検索対象ディレクトリ

解説

grepでは -E を使うことで OR 条件による複数パターン検索ができます。
また -e を複数指定する方法は、パターン追加がしやすく可読性も高いです。

grepの複数パターンで検索対象ディレクトリを除外する方法

ファイル作成

mkdir -p project/src

ファイル作成

cat << 'EOF' > project/app.log INFO: Application started ERROR: Database connection failed WARN: Retry processing EOF

ファイル作成

cat << 'EOF' > project/error.log FATAL: System crash ERROR: Memory leak detected EOF

ファイル作成

cat << 'EOF' > project/src/main.txt INFO: User login success DEBUG: Session created EOF

ファイル作成

cat << 'EOF' > project/src/debug.txt TRACE: API request WARN: Slow response detected EOF

実行コマンド

grep -r -E "ERROR|WARN" --exclude-dir="project/src" project

実行結果

project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected

実行コマンド

grep -r -E "ERROR|WARN" project

実行結果

project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected
project/src/debug.txt:WARN: Slow response detected

仕組み

オプション 内容
-r ディレクトリを再帰的に検索する
-E 拡張正規表現を使用する
"ERROR|WARN" ERROR または WARN に一致する行を検索する
--exclude-dir="project/src" project/src ディレクトリを検索対象から除外する

解説

grepコマンドでは -E を使用することで複数パターンを OR 条件で検索できます。
また、--exclude-dir オプションを利用すると、特定のディレクトリを検索対象から除外できます。
ログ検索時に不要なディレクトリを除外することで、検索速度向上やノイズ削減に役立ちます。

grepの複数パターンとfindコマンドを連携する方法

ファイル作成

cat << 'EOF' > input.txt logs/app.log:ERROR Database connection failed logs/app.log:INFO User login success logs/system.log:WARNING Disk usage high logs/system.log:ERROR Memory leak detected backup/data.txt:INFO Backup completed backup/data.txt:ERROR Backup failed EOF

ファイル作成

mkdir -p logs backup

ファイル作成

cp input.txt logs/app.log

ファイル作成

cp input.txt logs/system.log

ファイル作成

cp input.txt backup/data.txt

実行コマンド

find logs backup -type f | xargs grep -E 'ERROR|WARNING'

実行結果

logs/app.log:logs/app.log:ERROR Database connection failed
logs/app.log:logs/system.log:WARNING Disk usage high
logs/app.log:logs/system.log:ERROR Memory leak detected
logs/app.log:backup/data.txt:ERROR Backup failed
logs/system.log:logs/app.log:ERROR Database connection failed
logs/system.log:logs/system.log:WARNING Disk usage high
logs/system.log:logs/system.log:ERROR Memory leak detected
logs/system.log:backup/data.txt:ERROR Backup failed
backup/data.txt:logs/app.log:ERROR Database connection failed
backup/data.txt:logs/system.log:WARNING Disk usage high
backup/data.txt:logs/system.log:ERROR Memory leak detected
backup/data.txt:backup/data.txt:ERROR Backup failed

仕組み

コマンド 役割
find logs backup -type f logs と backup 配下のファイルを検索
xargs grep -E 'ERROR|WARNING' 複数パターンをOR条件でgrep検索
-E 拡張正規表現を有効化
ERROR|WARNING ERROR または WARNING に一致

解説

findで対象ディレクトリを限定することで、不要なファイル検索を避けながら効率よくgrep検索を実行できます。
grep -Eを使うことで、複数キーワードをOR条件でまとめて検索可能です。

grepの複数パターンでログ解析を効率化する方法

ファイル作成

cat << 'EOF' > input.txt 2026-05-20 10:00:01 INFO User login success 2026-05-20 10:01:15 ERROR Database connection failed 2026-05-20 10:02:30 WARN Disk usage above 80% 2026-05-20 10:03:45 INFO Scheduled backup completed 2026-05-20 10:04:50 ERROR Timeout while calling API 2026-05-20 10:05:20 WARN Memory usage high EOF

実行コマンド

grep -E 'ERROR|WARN' input.txt

実行結果

2026-05-20 10:01:15 ERROR Database connection failed
2026-05-20 10:02:30 WARN Disk usage above 80%
2026-05-20 10:04:50 ERROR Timeout while calling API
2026-05-20 10:05:20 WARN Memory usage high

実行コマンド

grep -e 'ERROR' -e 'WARN' input.txt

実行結果

2026-05-20 10:01:15 ERROR Database connection failed
2026-05-20 10:02:30 WARN Disk usage above 80%
2026-05-20 10:04:50 ERROR Timeout while calling API
2026-05-20 10:05:20 WARN Memory usage high

仕組み

方法 コマンド例 仕組み
-E オプション grep -E 'ERROR|WARN' input.txt 拡張正規表現を使い、|で複数条件を指定
-e オプション grep -e 'ERROR' -e 'WARN' input.txt -e を複数指定して複数条件を検索

解説

grepの複数パターンを使うことで、大量ログから複数条件を一度に抽出できます。
障害解析や監視ログ確認の効率化に非常に有効です。

grepの複数パターンでよくあるエラーと対処法

ファイル作成

cat << 'EOF' > input.txt error: failed login warning: disk usage high info: backup completed error: timeout detected warning: memory leak suspected EOF

実行コマンド

grep -E 'error|warning' input.txt

実行結果

error: failed login
warning: disk usage high
error: timeout detected
warning: memory leak suspected

実行コマンド

grep 'error|warning' input.txt

実行結果

(出力なし)

実行コマンド

grep -e 'error' -e 'warning' input.txt

実行結果

error: failed login
warning: disk usage high
error: timeout detected
warning: memory leak suspected

仕組み

仕組み 内容
grep -E 'error|warning' 拡張正規表現を有効化し、| によるOR検索を行う
grep 'error|warning' 通常のgrepでは | がORとして解釈されない
grep -e 'error' -e 'warning' -eを複数指定してOR検索を実現する

解説

grepの複数パターンでは、|を使う際に -E を忘れるエラーが非常に多いです。
確実に動作させたい場合は grep -e を複数使う方法もよく利用されます。

grepの複数パターン検索を効率化する活用ポイントまとめ

grepの複数パターン検索を理解すると、OR検索やAND検索、除外検索まで柔軟に対応できます。

さらにfindコマンドとの連携や再帰検索を組み合わせれば、大規模なログ解析や設定確認も効率化できます。

まずは基本的なパターン指定から試し、用途に応じてオプションを使い分けることが重要です。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

©︎ 2025-2026 running terminal commands