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

grepのand条件を徹底解説

updated: 2026/05/27 created: 2026/05/27

はじめに

grepはLinuxやMacで文字列検索を行う定番コマンドです。

特にand条件を使えるようになると、ログ解析や複数ファイル検索の効率が大きく向上します。

本記事では、grepのand条件を初学者向けにわかりやすく整理し、実践で役立つ検索方法を解説します。

参考:GNU grep

grepのand条件の基本構文

ファイル作成

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

実行コマンド

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

実行結果

apple orange banana
apple orange

仕組み

項目 内容
1つ目のgrep apple を含む行を抽出
パイプ| 前のコマンドの結果を次のコマンドへ渡す
2つ目のgrep orange を含む行をさらに抽出
AND条件 両方の文字列を含む行のみ表示

解説

grep の AND 条件は、パイプで複数の grep を組み合わせることで実現できます。前段の検索結果を後段でさらに絞り込むのが基本的な仕組みです。

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 grep "apple" で apple を含む行を抽出
パイプ | 前の検索結果を次のgrepへ渡す
2つ目のgrep grep "orange" で orange を含む行だけをさらに抽出
AND条件 複数のgrepを連結することでAND検索を実現

解説

grep は単体では OR 条件が使われることが多いですが、パイプで連結すると AND 条件を実現できます。

複数キーワードを順番に絞り込むことで、目的の行だけを効率よく抽出できます。

grepのand条件をパイプで実現する方法

ファイル作成

cat << 'EOF' > input.txt apple red fresh apple green sour banana yellow fresh orange orange sweet apple red sweet EOF

実行コマンド

grep 'apple' input.txt | grep 'red'

実行結果

apple red fresh
apple red sweet

仕組み

項目 内容
1回目のgrep grep 'apple' input.txt で apple を含む行を抽出
パイプ | 前のgrep結果を次のgrepへ渡す
2回目のgrep grep 'red' でさらに red を含む行だけ抽出
結果 apple かつ red を含むAND条件が実現できる

解説

grep をパイプで連結すると、前段の検索結果に対してさらに条件を追加できます。
複数条件を段階的に絞り込めるため、シンプルで可読性の高い方法です。

grepのand条件を正規表現で実現する方法

ファイル作成

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

実行コマンド

grep -E 'apple.*orange|orange.*apple' input.txt

実行結果

apple orange banana
apple orange

仕組み

項目 内容
grep テキスト検索を行うコマンド
-E 拡張正規表現を有効化
apple.*orange appleの後にorangeが存在する行を検索
orange.*apple orangeの後にappleが存在する行を検索
| 正規表現でOR条件を表現
AND条件の実現 ORで順不同パターンを組み合わせて実現

解説

grep単体にはAND演算子はありませんが、正規表現を組み合わせることでAND条件を実現できます。
順不同に対応するため、両方の並び順をOR条件で指定するのがポイントです。

grepのand条件で1行内に複数文字列を含む行を検索する方法

ファイル作成

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

実行コマンド

grep 'apple' input.txt

実行結果

apple orange banana
apple grape
apple orange

実行コマンド

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

実行結果

apple orange banana
apple orange

仕組み

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

解説

grep はパイプを組み合わせることでAND条件を実現できます。

複数条件検索はログ解析や設定ファイル調査でよく利用されます。

grepのand条件とor条件の違い

ファイル作成

cat << 'EOF' > input.txt apple red apple green banana yellow orange orange apple yellow EOF

実行コマンド

grep 'apple' input.txt | grep 'yellow'

実行結果

apple yellow

実行コマンド

grep -E 'apple|banana' input.txt

実行結果

apple red
apple green
banana yellow
apple yellow

仕組み

条件 コマンド例 仕組み 結果
AND条件 grep 'apple' input.txt | grep 'yellow' 1回目のgrep結果を2回目のgrepでさらに絞り込む appleかつyellowを含む行
OR条件 grep -E 'apple|banana' input.txt -Eで拡張正規表現を有効化し|で複数条件を指定 appleまたはbananaを含む行

解説

grep and 条件 は、パイプ | を使って複数回grepすることで実現します。
一方、OR条件は grep -E と | を使うことで簡潔に記述できます。

grepのand条件とnot条件を組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt error: database connection failed info: user login success warning: disk usage high error: timeout occurred debug: retry process started EOF

実行コマンド

grep "error" input.txt | grep -v "timeout"

実行結果

error: database connection failed

仕組み

条件 コマンド 内容
AND条件 grep "error" input.txt | grep "failed" error と failed の両方を含む行を抽出
NOT条件 grep -v "timeout" timeout を含む行を除外
AND + NOT条件 grep "error" input.txt | grep -v "timeout" error を含み、timeout を含まない行を抽出

解説

grep はパイプ(|)で複数条件を組み合わせることでAND条件を実現できます。

grep -v を使うとNOT条件として特定文字列を除外できます。

grepのand条件で複数条件を-eオプションで指定する方法

ファイル作成

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

実行コマンド

grep -e "apple" -e "orange" input.txt

実行結果

apple orange banana
apple grape
orange lemon
apple orange

実行コマンド

grep -e "apple" -e "orange" input.txt | grep "apple"

実行結果

apple orange banana
apple grape
apple orange

実行コマンド

grep -e "apple" -e "orange" input.txt | grep "apple" | grep "orange"

実行結果

apple orange banana
apple orange

仕組み

項目 内容
-e オプション 複数の検索パターンを指定できる
grep -e "apple" -e "orange" apple または orange を含む行を抽出
grep "apple" apple を含む行だけに絞る
grep "orange" orange を含む行だけに絞る
AND条件の実現 grep をパイプで連結して両方に一致させる

解説

grep の -e オプションを使うことで複数条件を柔軟に指定できます。

AND条件は grep をパイプで連結することで実現できます。

grepのand条件で複数ファイルを検索する方法

ファイル作成

cat << 'EOF' > file1.txt ERROR: database connection failed INFO: retry started WARN: timeout detected EOF

ファイル作成

cat << 'EOF' > file2.txt INFO: process started ERROR: timeout while reading response INFO: process finished EOF

ファイル作成

cat << 'EOF' > file3.txt WARN: disk usage high ERROR: timeout detected in backup EOF

実行コマンド

grep -l "ERROR" file*.txt | xargs grep -l "timeout"

実行結果

file1.txt
file2.txt
file3.txt

仕組み

仕組み 内容
grep -l "ERROR" file*.txt ERROR を含むファイル名だけを抽出
xargs grep -l "timeout" 抽出したファイルに対してさらに timeout を検索
AND条件 2つの grep 条件を順番に適用することで AND検索を実現

解説

grep 単体にはAND演算子がないため、検索結果を次の grep に渡して実現します。

複数ファイル検索では xargs を組み合わせることで効率よく絞り込みできます。

grepのand条件でディレクトリ配下を再帰検索する方法

ファイル作成

cat << 'EOF' > input.txt project/app.log:ERROR Database connection failed project/app.log:INFO User login success project/server.log:ERROR Timeout occurred project/server.log:WARN Memory usage high project/debug.log:ERROR Disk full project/debug.log:INFO Retry process started EOF

ファイル作成

mkdir -p project && while IFS=: read -r file content; do echo "$content" >> "$file"; done < input.txt

実行コマンド

grep -r "ERROR" project | grep "Timeout"

実行結果

project/server.log:ERROR Timeout occurred

仕組み

項目 内容
grep -r ディレクトリ配下を再帰的に検索する
grep "ERROR" ERRORを含む行を抽出する
| grep "Timeout" Timeoutを含む行だけをさらに抽出する
AND条件 複数grepをパイプでつなぐことで実現する

解説

grep は単体ではOR検索が中心ですが、パイプで連結することでAND条件検索が可能です。

grep -r を使うことで、ディレクトリ配下のファイルをまとめて検索できます。

grepのand条件でログ解析する方法

ファイル作成

cat << 'EOF' > input.txt 2026-05-26 10:00:01 INFO User login success 2026-05-26 10:01:15 ERROR Database connection failed 2026-05-26 10:02:30 ERROR Timeout while connecting API 2026-05-26 10:03:45 INFO Scheduled batch started 2026-05-26 10:04:12 ERROR Database timeout detected 2026-05-26 10:05:20 WARN Disk usage high EOF

実行コマンド

grep 'ERROR' input.txt | grep 'Database'

実行結果

2026-05-26 10:01:15 ERROR Database connection failed
2026-05-26 10:04:12 ERROR Database timeout detected

実行コマンド

grep -E 'ERROR.*Database|Database.*ERROR' input.txt

実行結果

2026-05-26 10:01:15 ERROR Database connection failed
2026-05-26 10:04:12 ERROR Database timeout detected

仕組み

方法 コマンド 仕組み
grepをパイプで連結 grep 'ERROR' input.txt | grep 'Database' 1回目のgrep結果を2回目のgrepでさらに絞り込み、AND条件を実現
正規表現を利用 grep -E 'ERROR.*Database|Database.*ERROR' input.txt 1行内に両方のキーワードが含まれる行を抽出

解説

grepのAND条件は、パイプで複数回grepを実行する方法が最もシンプルです。
1行内に複数キーワードが存在するか確認したい場合は、正規表現も便利です。

grepのand条件でfindコマンドを組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt apple red apple green banana yellow grape purple apple yellow EOF

実行コマンド

find . -name "input.txt" -type f -exec grep -E "apple" {} \; | grep "yellow"

実行結果

apple yellow

仕組み

コマンド 役割
find . -name "input.txt" 対象ファイルを検索
grep -E "apple" 1つ目の条件で絞り込み
grep "yellow" 2つ目の条件でさらに絞り込み
| (パイプ) grep結果を次のgrepへ渡してAND条件を実現

解説

grep単体ではAND条件を直接扱いづらいため、パイプで複数のgrepを連結します。

findと組み合わせることで、複数ファイル検索時にも柔軟にAND条件検索が可能です。

grepのand条件で大量ログを効率的に解析する方法

ファイル作成

cat << 'EOF' > input.txt 2026-05-26 10:00:01 INFO user_id=101 status=SUCCESS action=login 2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment 2026-05-26 10:00:08 ERROR user_id=103 status=FAILED action=login 2026-05-26 10:00:10 INFO user_id=104 status=SUCCESS action=logout 2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment 2026-05-26 10:00:20 WARN user_id=106 status=RETRY action=payment EOF

実行コマンド

grep "ERROR" input.txt | grep "payment"

実行結果

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

実行コマンド

grep -E "ERROR.*payment" input.txt

実行結果

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

実行コマンド

awk '/ERROR/ && /payment/' input.txt

実行結果

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

仕組み

方法 コマンド例 仕組み 特徴
grepパイプ grep "ERROR" input.txt | grep "payment" 1回目のgrep結果を2回目で絞り込む シンプルで理解しやすい
grep正規表現 grep -E "ERROR.*payment" input.txt 正規表現でAND条件を1回で判定 高速で大量ログ向き
awk条件式 awk '/ERROR/ && /payment/' input.txt awkの論理ANDで複数条件を判定 条件追加が柔軟

解説

grep and 条件では、パイプや正規表現を使うことで複数キーワードを効率的に抽出できます。

大量ログ解析では grep -E や awk を使うと処理回数を減らせるため高速です。

grepのand条件の応用テクニック

ファイル作成

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

実行コマンド

grep 'error' input.txt | grep 'memory'

実行結果

error: memory leak detected

実行コマンド

grep -E 'error.*memory|memory.*error' input.txt

実行結果

error: memory leak detected

実行コマンド

grep 'warning' input.txt | grep 'network'

実行結果

warning: network unstable

仕組み

コマンド 仕組み
grep 'error' input.txt | grep 'memory' 1回目のgrepでerrorを含む行を抽出し、その結果に対して2回目のgrepでmemoryを含む行を絞り込む
grep -E 'error.*memory|memory.*error' input.txt 拡張正規表現を使い、1回のgrepでerrorとmemoryの両方を含む行を検索する
grep 'warning' input.txt | grep 'network' パイプを利用して複数条件をAND検索している

解説

grepのAND条件は、パイプで複数のgrepを連結する方法が基本です。
さらにgrep -Eを使うことで、正規表現による柔軟な条件指定も可能になります。

grepのand条件を理解して検索効率を高めるまとめ

grepのand条件は、複数の条件を組み合わせて必要な情報だけを抽出できる便利な手法です。

パイプを使った基本的な方法から、正規表現やfindコマンドとの組み合わせまで覚えることで、ログ解析や障害調査の作業効率が大きく向上します。

まずはシンプルなand条件から試し、徐々に複数ファイル検索や再帰検索などの応用へ広げることが、初学者にとって理解しやすい学習方法です。

コメントを残す

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

©︎ 2025-2026 running terminal commands