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

grepとパイプを徹底解説

updated: 2026/08/02 created: 2026/08/02

はじめに

grepとパイプは、LinuxやUnix系OSで文字列検索を効率化するために欠かせないコマンドです。

パイプを使えば複数のコマンドを連携できます。

本記事では、grepとパイプの基本構文から応用までを詳しく解説します。

参考:GNU grep

grepとパイプの基本構文

ファイル作成

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

実行コマンド

cat input.txt | grep "apple"

実行結果

apple
apple pie
pineapple
apple juice

実行コマンド

grep "apple" input.txt | grep "juice"

実行結果

apple juice

仕組み

項目 内容
cat input.txt ファイルの内容を標準出力へ表示する
|(パイプ) 前のコマンドの出力を次のコマンドの入力として渡す
grep "apple" apple を含む行だけを抽出する
grep "juice" 前段の結果からさらに juice を含む行だけを抽出する
パイプのメリット 複数のコマンドを組み合わせて効率的にデータを絞り込める

解説

grep は条件に一致する行を抽出するコマンドです。
パイプ(|)と組み合わせることで、複数の条件を順番に適用し、必要なデータだけを効率よく取り出せます。

grepとパイプで標準入力を検索する方法

ファイル作成

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

実行コマンド

cat input.txt | grep "apple"

実行結果

apple
apple pie
pineapple

実行コマンド

echo -e "dog\ncat\nbird\ncatfish" | grep "cat"

実行結果

cat
catfish

仕組み

項目 内容
cat input.txt ファイルの内容を標準出力へ表示する
| 左側コマンドの標準出力を右側コマンドの標準入力へ渡す
grep "apple" 標準入力から apple を含む行だけを抽出する
処理の流れ cat → パイプ → grep の順でデータが受け渡される

解説

パイプ(|)を使うと、前のコマンドの出力をそのまま grep の入力として利用できます。

ファイルだけでなく、echo や他のコマンドの出力も同様に検索できるため、シェルでのデータ処理を効率化できます。

grepとパイプでcatコマンドの出力を検索する方法

ファイル作成

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

実行コマンド

cat input.txt | grep "apple"

実行結果

apple
pineapple
apple juice

仕組み

要素 役割
cat input.txt input.txt の内容を標準出力へ表示する
| (パイプ) 前のコマンドの標準出力を次のコマンドの標準入力へ渡す
grep "apple" 入力されたテキストから apple を含む行だけを抽出する

解説

パイプ(|)を使うことで、cat の出力をそのまま grep に渡して検索できます。

複数のコマンドを組み合わせることで、効率よくテキストを検索・加工できます。

grepとパイプで複数条件を絞り込む方法

ファイル作成

cat << 'EOF' > input.txt INFO: Server started ERROR: Disk full INFO: User login ERROR: Network timeout WARNING: High memory usage ERROR: Disk read failure INFO: Backup completed EOF

実行コマンド

grep "ERROR" input.txt | grep "Disk"

実行結果

ERROR: Disk full
ERROR: Disk read failure

実行コマンド

grep "INFO" input.txt | grep "Backup"

実行結果

INFO: Backup completed

仕組み

コマンド 役割
grep "ERROR" input.txt ERROR を含む行だけを抽出する
| 前のコマンドの出力を次のコマンドへ渡す(パイプ)
grep "Disk" パイプで受け取った結果から Disk を含む行だけを抽出する
grep "INFO" input.txt | grep "Backup" INFO を含む行の中から Backup を含む行だけを抽出する

解説

パイプ(|)を使うことで、grep の検索結果を次の grep に渡し、複数条件で段階的に絞り込めます。

複雑な検索条件でもシンプルなコマンドの組み合わせで実現できます。

grepとパイプで除外検索を行う方法

ファイル作成

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

実行コマンド

grep "a" input.txt | grep -v "banana"

実行結果

apple
orange
grape
orange juice

仕組み

要素 内容
grep "a" input.txt a を含む行を抽出する
| 前のコマンドの結果を次のコマンドへ渡す(パイプ)
grep -v "banana" banana を含む行を除外する
最終結果 a を含み、かつ banana を含まない行だけを表示する

解説

grep とパイプを組み合わせることで、検索結果をさらに絞り込めます。

grep -v は条件に一致する行を除外したい場合によく使用されます。

grepとパイプで正規表現を使って柔軟に検索する方法

ファイル作成

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

実行コマンド

cat input.txt | grep -E '^apple|banana'

実行結果

apple
banana
apple pie
banana split

実行コマンド

cat input.txt | grep -Ei 'apple|orange'

実行結果

apple
orange
apple pie
pineapple
Apple

実行コマンド

cat input.txt | grep -Ev 'apple|banana'

実行結果

orange
grape
Apple

仕組み

要素 説明
cat input.txt ファイルの内容を標準出力へ出力する
| 前のコマンドの出力を次のコマンドへ渡す(パイプ)
grep 条件に一致する行を検索する
-E 拡張正規表現を有効にし、apple|banana のようなOR検索を利用できる
-i 大文字・小文字を区別せず検索する
-v 一致した行ではなく、一致しない行を表示する
^apple 行頭が apple の行に一致する
apple|orange appleまたはorangeに一致する正規表現

解説

パイプを利用すると、前のコマンドの出力をそのまま grep に渡して検索できます。
grep -E と正規表現を組み合わせることで、OR検索や行頭指定など柔軟な条件で効率よく検索できます。

grepとパイプでhead・tailを組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt error: failed to connect info: server started warning: low memory error: permission denied info: user login error: disk full warning: cpu usage high info: backup completed error: network timeout info: shutdown completed EOF

実行コマンド

grep "error" input.txt | head -n 2

実行結果

error: failed to connect
error: permission denied

実行コマンド

grep "error" input.txt | tail -n 2

実行結果

error: disk full
error: network timeout

仕組み

要素 説明
grep "error" input.txt error を含む行だけを抽出する
| (パイプ) grep の実行結果を次のコマンドへ渡す
head -n 2 パイプで受け取った先頭2行を表示する
tail -n 2 パイプで受け取った末尾2行を表示する

解説

grep の検索結果はパイプ(|)を使って head や tail に渡せます。
大量の検索結果から先頭や末尾だけを確認したい場合に便利です。

grepとパイプでsort・uniqを組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt INFO Login success ERROR Database error INFO File uploaded WARN Disk space low ERROR Network timeout INFO Login success ERROR Database error INFO Logout WARN Disk space low INFO File uploaded EOF

実行コマンド

grep "ERROR" input.txt | sort | uniq

実行結果

ERROR Database error
ERROR Network timeout

実行コマンド

grep "INFO" input.txt | sort | uniq

実行結果

INFO File uploaded
INFO Login success
INFO Logout

実行コマンド

grep -E "INFO|WARN" input.txt | sort | uniq

実行結果

INFO File uploaded
INFO Login success
INFO Logout
WARN Disk space low

仕組み

コマンド 役割
grep 条件に一致する行だけを抽出する
|(パイプ) 前のコマンドの実行結果を次のコマンドへ渡す
sort 抽出した行を並び替える
uniq 連続する重複行を1つにまとめる
grep "ERROR" input.txt | sort | uniq ERROR行を抽出し、並び替えた後に重複を削除する

解説

grepで必要な行だけを抽出し、その結果をパイプでsortへ渡すことで重複行を並べ替えられます。
最後にuniqを組み合わせることで、同じ内容を1件だけ表示できます。

grepとパイプでwcを組み合わせて件数を数える方法

ファイル作成

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

実行コマンド

grep "apple" input.txt | wc -l

実行結果

4

仕組み

要素 役割
grep "apple" input.txt input.txtからappleを含む行だけを抽出する
| grepの出力を次のコマンドへ渡す(パイプ)
wc -l 渡された行数を数える
実行結果 appleを含む行が4件あるため4を出力する

解説

grepで条件に一致する行だけを抽出し、その結果をパイプ(|)でwc -lへ渡すことで一致件数を簡単に数えられます。
ログ解析やテキスト検索で頻繁に利用される組み合わせです。

grepとパイプでawkを組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt INFO: Server started ERROR: Disk full INFO: User login ERROR: Connection timeout WARN: High memory usage ERROR: Permission denied EOF

実行コマンド

grep "ERROR" input.txt | awk '{print $2, $3}'

実行結果

Disk full
Connection timeout
Permission denied

実行コマンド

grep "INFO" input.txt | awk '{print NR ":", $2, $3}'

実行結果

1: Server started
2: User login

仕組み

要素 役割
grep "ERROR" input.txt ERROR を含む行だけを抽出する
awk '{print $2, $3}' 抽出された行の2列目・3列目だけを表示する
NR awkで処理中の行番号を表す組み込み変数

解説

grepで必要な行だけを抽出し、その結果をパイプでawkへ渡すことで効率よくデータを加工できます。
ログ解析やテキストファイルの集計でよく利用される組み合わせです。

grepとパイプでfindコマンドの検索結果をさらに絞り込む方法

ファイル作成

cat << 'EOF' > input.txt error: failed to connect info: service started warning: disk usage 80% error: timeout occurred info: backup completed warning: memory usage high error: permission denied EOF

実行コマンド

find . -name "*.txt" | grep "input"

実行結果

./input.txt

実行コマンド

find . -name "*.txt" | grep "input" | xargs grep "error"

実行結果

./input.txt:error: failed to connect
./input.txt:error: timeout occurred
./input.txt:error: permission denied

仕組み

要素 役割
find . -name "*.txt" カレントディレクトリ以下から .txt ファイルを検索する
| 前のコマンドの標準出力を次のコマンドへ渡す(パイプ)
grep "input" find の検索結果から input を含むファイル名だけを抽出する
xargs grep "error" 抽出したファイルを grep に渡し、error を含む行だけを表示する

解説

find の検索結果をパイプで grep に渡すことで、目的のファイルだけを効率よく絞り込めます。
さらに xargs grep を組み合わせることで、対象ファイル内の特定の文字列もまとめて検索できます。

grepとパイプでログファイルを効率よく解析する方法

ファイル作成

cat << 'EOF' > input.txt 2026-08-01 10:00:01 INFO Application started 2026-08-01 10:00:15 INFO User login: alice 2026-08-01 10:01:22 ERROR Database connection failed 2026-08-01 10:01:45 WARN Retry connecting to database 2026-08-01 10:02:03 INFO Database connection established 2026-08-01 10:02:30 ERROR Failed to load configuration 2026-08-01 10:03:11 INFO User logout: alice 2026-08-01 10:03:40 ERROR Disk space is low EOF

実行コマンド

grep "ERROR" input.txt

実行結果

2026-08-01 10:01:22 ERROR Database connection failed
2026-08-01 10:02:30 ERROR Failed to load configuration
2026-08-01 10:03:40 ERROR Disk space is low

実行コマンド

grep "ERROR" input.txt | wc -l

実行結果

3

実行コマンド

grep "ERROR" input.txt | grep "Database"

実行結果

2026-08-01 10:01:22 ERROR Database connection failed

実行コマンド

grep "INFO" input.txt | cut -d' ' -f4-

実行結果

Application started
User login: alice
Database connection established
User logout: alice

仕組み

コマンド 役割
grep "ERROR" input.txt ERROR を含む行だけを抽出する
| 前のコマンドの出力を次のコマンドへ渡す(パイプ)
wc -l 渡された行数を数える
grep "Database" パイプで受け取った結果をさらに絞り込む
cut -d' ' -f4- スペース区切りで4列目以降を表示する

解説

grepとパイプ(|)を組み合わせることで、ログから必要な情報だけを段階的に抽出できます。
複数のコマンドを連携させることで、大量のログでも効率よく解析できるようになります。

grepとパイプでパフォーマンスを改善する方法

ファイル作成

cat << 'EOF' > input.txt INFO: Server started ERROR: Database connection failed INFO: User login WARNING: Disk usage 85% ERROR: File not found INFO: Backup completed ERROR: Permission denied INFO: Server stopped EOF

実行コマンド

cat input.txt | grep "ERROR"

実行結果

ERROR: Database connection failed
ERROR: File not found
ERROR: Permission denied

実行コマンド

grep "ERROR" input.txt | wc -l

実行結果

3

実行コマンド

grep "ERROR" input.txt | sort

実行結果

ERROR: Database connection failed
ERROR: File not found
ERROR: Permission denied

仕組み

項目 内容
grep 指定した文字列を含む行だけを抽出する
パイプ (|) 前のコマンドの出力を次のコマンドへ渡す
パフォーマンス改善 必要なデータだけを次のコマンドへ渡すため、不要な処理量を削減できる
利点 コマンドを組み合わせることで高速かつ効率的にログ解析やデータ検索を行える

解説

grepを先に実行して必要な行だけを抽出してから後続のコマンドへ渡すことで、処理対象のデータ量を減らし効率的に処理できます。
ログ解析や大量データの検索で特に効果を発揮します。

grepとパイプを使う際によくあるエラーと対処法

ファイル作成

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

実行コマンド

cat input.txt | grep "apple"

実行結果

apple
apple pie
pineapple

実行コマンド

cat input.txt | grep "melon"

実行結果

no output

実行コマンド

cat input.txt | grep -i "APPLE"

実行結果

apple
apple pie
pineapple

仕組み

コマンド 仕組み よくあるエラー 対処法
cat input.txt | grep "apple" catの標準出力をパイプ(|)でgrepへ渡し、一致した行だけ表示する。 grep: command not found grepがインストールされているか確認する。
cat input.txt | grep "melon" 一致する文字列がないため、何も出力されない。 「動作していない」と勘違いする 検索文字列や入力データを確認する。
cat input.txt | grep -i "APPLE" -iで大文字・小文字を区別せず検索する。 Appleとappleが一致しない -iオプションを利用する。

解説

grepはパイプから受け取った標準入力も検索できます。

一致しない場合はエラーではなく正常終了であることが多いため、検索条件やオプションを確認することが重要です。

grepとパイプで学ぶ実践的な使い方のまとめ

grepとパイプを理解すると、日常的な検索作業やログ解析を効率よく進められます。

基本構文を身に付けたうえで、各コマンドとの組み合わせや正規表現を活用すれば、目的の情報を素早く抽出できます。

まずは基本的な使い方から試し、少しずつ実践的な操作へ広げていくことがおすすめです。

コメントを残す

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

©︎ 2025-2026 running terminal commands