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

grepの除外を徹底解説

updated: 2026/06/20 created: 2026/06/20

はじめに

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

ログ解析や設定ファイルの確認では、不要な行を除外しながら検索したい場面がよくあります。

しかし、初学者の方はオプションや正規表現の使い方でつまずきやすい傾向があります。

本記事では、grepの除外に関する基本から実践までを分かりやすく解説します。

参考: GNU grep

grepで除外する基本構文と使い方

ファイル作成

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

実行コマンド

grep -v "banana" input.txt

実行結果

apple
orange
grape
apple pie

実行コマンド

grep -v "apple" input.txt

実行結果

banana
orange
grape
banana split

実行コマンド

grep -Ev "apple|banana" input.txt

実行結果

orange
grape

仕組み

構文 説明
grep "文字列" ファイル 指定文字列を含む行を表示
grep -v "文字列" ファイル 指定文字列を含む行を除外して表示
grep -E "A|B" ファイル 複数条件(OR検索)
grep -Ev "A|B" ファイル 複数条件に一致する行を除外
grep -vi "文字列" ファイル 大文字・小文字を区別せず除外

解説

grep 除外 は -v オプションを使用することで実現できます。
-E を組み合わせると複数のパターンをまとめて除外できるため、ログ解析やテキスト抽出でよく利用されます。

grepで除外検索する-vオプションの意味

ファイル作成

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

実行コマンド

grep -v "error" input.txt

実行結果

apple
orange
banana
grape
melon

実行コマンド

grep -v "banana" input.txt

実行結果

apple
orange
error
grape
error_log
melon

実行コマンド

grep -v -e "error" -e "banana" input.txt

実行結果

apple
orange
grape
melon

仕組み

項目 内容
grep ファイル内の文字列を検索するコマンド
-v 条件に一致した行を除外して表示する
通常検索 一致した行のみ表示する
除外検索 一致した行以外を表示する
複数除外 -e を組み合わせて複数条件を除外できる

解説

grep -v は検索条件に一致した行を結果から除外するオプションです。
ログファイルから不要なメッセージを除外したい場合など、grep 除外 の用途でよく利用されます。

grepで特定の文字列を除外する方法

ファイル作成

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

実行コマンド

grep -v "apple" input.txt

実行結果

banana
orange
grape

実行コマンド

grep -Ev "apple|orange" input.txt

実行結果

banana
grape

実行コマンド

grep -Fv "apple pie" input.txt

実行結果

apple
banana
orange
grape

仕組み

オプション 説明
-v マッチした行を除外して表示する grep -v "apple" input.txt
-E 拡張正規表現を使用する grep -Ev "apple|orange" input.txt
-F 正規表現ではなく固定文字列として検索する grep -Fv "apple pie" input.txt
apple|orange apple または orange に一致する行を除外 grep -Ev "apple|orange" input.txt

解説

grep 除外 を行う場合は -v オプションを使用します。

複数の文字列を除外したい場合は -E と | を組み合わせることで効率的に実現できます。

grepで複数キーワードを除外する方法

ファイル作成

cat << 'EOF' > input.txt grep grep 除外 awk sed grep 除外 パターン find EOF

実行コマンド

grep -v -E 'grep|除外' input.txt

実行結果

awk
sed
find

実行コマンド

grep -v -e 'grep' -e '除外' input.txt

実行結果

awk
sed
find

仕組み

項目 内容
grep -v マッチした行を除外して表示する
-E 拡張正規表現を利用する
grep|除外 grep または 除外 を含む行にマッチする
-e 除外したいキーワードを複数指定できる
処理結果 指定したキーワードを含む行を出力対象から除外する

解説

grep -v を利用すると、指定したキーワードに一致する行を除外できます。
複数キーワードを除外する場合は -E の OR 条件 (|) または -e を複数指定する方法が便利です。

grepで正規表現を使って除外する方法

ファイル作成

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

実行コマンド

grep -v 'banana' input.txt

実行結果

apple
orange
grape
pineapple

実行コマンド

grep -Ev 'apple|orange' input.txt

実行結果

banana
grape

実行コマンド

grep -Ev 'apple$' input.txt

実行結果

banana
orange
grape

仕組み

オプション/正規表現 説明
-v マッチした行を除外して表示する
-E 拡張正規表現を有効にする
apple|orange apple または orange にマッチする
apple$ apple で終わる行にマッチする
^apple apple で始まる行にマッチする

解説

grep の除外は -v オプションを使用します。
正規表現と組み合わせることで、複数条件や行頭・行末指定など柔軟な除外検索が可能です。

grepで大文字・小文字を無視して除外する方法

ファイル作成

cat << 'EOF' > input.txt Linux linux LINUX Ubuntu CentOS ubuntu EOF

実行コマンド

grep -iv "linux" input.txt

実行結果

Ubuntu
CentOS
ubuntu

実行コマンド

grep -iv "ubuntu" input.txt

実行結果

Linux
linux
LINUX
CentOS

仕組み

オプション 説明
grep テキストを検索するコマンド
-i 大文字・小文字を区別せずに検索する
-v 一致した行を除外して表示する
grep -iv "linux" input.txt linux に一致する行(Linux、linux、LINUX など)を除外する

解説

grep 除外 は -v オプションを使用します。
さらに -i を組み合わせることで、大文字・小文字を無視して一致した行を除外できます。

grepで空行を除外する方法

ファイル作成

cat << 'EOF' > input.txt aaa bbb ccc EOF

実行コマンド

grep -v '^$' input.txt

実行結果

aaa
bbb
ccc

仕組み

項目 内容
grep テキスト検索を行うコマンド
-v マッチした行を除外する(grep 除外)
^$ 空行を表す正規表現(行頭から行末まで文字がない)
処理内容 空行に一致する行を除外して表示する

解説

grep -v '^$' は空行に一致した行を除外して出力します。
ログや設定ファイルから不要な空行を取り除きたい場合によく利用されます。

grepでコメント行を除外する方法

ファイル作成

cat << 'EOF' > input.txt # コメント apple # 除外したいコメント banana orange # メモ grape EOF

実行コマンド

grep -v '^#' input.txt

実行結果

apple
banana
orange
grape

実行コマンド

grep -v '^#' input.txt | grep 'an'

実行結果

banana
orange

仕組み

項目 内容
grep ファイル内の文字列を検索するコマンド
-v マッチした行を除外して表示するオプション
^# 行頭(^)が#で始まるコメント行を表す正規表現
grep -v '^#' コメント行を除外して表示する
| 前のコマンドの出力を次のコマンドへ渡すパイプ

解説

grep -v を使用すると、指定したパターンに一致する行を簡単に除外できます。
コメント行を除外してから検索することで、設定ファイルやログを効率よく確認できます。

grepで完全一致する行だけを除外する方法

ファイル作成

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

実行コマンド

grep -vx 'orange' input.txt

実行結果

apple
banana
orange juice
grape

仕組み

オプション 説明
-v マッチした行を除外して表示する
-x 行全体が完全一致した場合のみマッチさせる
grep -vx 'orange' input.txt orange と完全一致する行だけを除外する
orange juice 完全一致ではないため除外されない

解説

grep -v だけでは部分一致した行も除外対象になります。

-x を組み合わせることで、指定文字列と完全一致する行だけを除外できます。

grepで複数条件を組み合わせて除外する方法

ファイル作成

cat << 'EOF' > input.txt INFO Start ERROR Connection failed WARN Disk usage high DEBUG Test message ERROR Database error INFO End EOF

実行コマンド

grep -Ev 'ERROR|DEBUG' input.txt

実行結果

INFO Start
WARN Disk usage high
INFO End

実行コマンド

grep -Ev 'ERROR|WARN' input.txt

実行結果

INFO Start
DEBUG Test message
INFO End

実行コマンド

grep -v -e 'ERROR' -e 'DEBUG' input.txt

実行結果

INFO Start
WARN Disk usage high
INFO End

仕組み

オプション 説明
-v マッチした行を除外して表示する
-E 拡張正規表現を有効にする
ERROR|DEBUG ERROR または DEBUG に一致
-e 検索条件を追加指定する
grep -Ev 'ERROR|DEBUG' 複数条件に一致する行をまとめて除外
grep -v -e 'ERROR' -e 'DEBUG' 条件を個別に指定して除外

解説

grep の除外は -v を使用します。
複数条件を指定する場合は -E と | を使う方法、または -e を複数指定する方法がよく利用されます。

grepで除外対象を単語単位で指定する方法

ファイル作成

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

実行コマンド

grep -vw 'apple' input.txt

実行結果

orange
grape
pineapple
orange juice

実行コマンド

grep -vw 'orange' input.txt

実行結果

apple
grape
pineapple
apple juice

実行コマンド

grep -vwE 'apple|orange' input.txt

実行結果

grape
pineapple

仕組み

オプション 内容
-v マッチした行を除外する(grep 除外)
-w 単語単位でマッチさせる
-E 拡張正規表現を使用する
apple 単語としての apple にマッチする
orange 単語としての orange にマッチする
apple|orange apple または orange にマッチする

解説

grep -v は一致した行を除外します。

-w を併用すると単語単位で判定されるため、apple juice や orange juice のように単語として含まれる行も一致対象となり除外されます。

一方で pineapple は別の単語として扱われるため除外されません。

grepで特定のファイルを除外して検索する方法

ファイル作成

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

ファイル作成

cat << 'EOF' > exclude.txt error: database connection failed EOF

実行コマンド

grep "error" input.txt

実行結果

error: database connection failed
error: timeout occurred

実行コマンド

grep "error" input.txt | grep -v -f exclude.txt

実行結果

error: timeout occurred

実行コマンド

grep --exclude="exclude.txt" "error" *.txt

実行結果

input.txt:error: database connection failed
input.txt:error: timeout occurred

仕組み

コマンド 仕組み
grep "error" input.txt input.txtからerrorを含む行を検索する
grep -v -f exclude.txt exclude.txtに記載されたパターンと一致する行を除外する
grep --exclude="exclude.txt" "error" *.txt 検索対象ファイルからexclude.txt自体を除外して検索する
-v マッチした行を除外(反転検索)する
--exclude 指定したファイル名を検索対象から除外する

解説

grepで除外を行う方法には、「検索結果から特定パターンを除外する (-v)」と「検索対象ファイル自体を除外する (--exclude)」の2種類があります。

用途に応じて使い分けることで効率よく検索できます。

grepで特定のディレクトリを除外する方法

ファイル作成

cat << 'EOF' > input.txt sample text error message warning message debug log error detected EOF

ファイル作成

mkdir -p logs/archive/

ファイル作成

cp input.txt logs/app.log

ファイル作成

cp input.txt logs/archive/old.log

実行コマンド

grep -r --exclude-dir=archive "error" logs

実行結果

logs/app.log:error message
logs/app.log:error detected

仕組み

項目 内容
grep -r ディレクトリを再帰的に検索する
--exclude-dir=archive archive ディレクトリを検索対象から除外する
"error" 検索する文字列
logs 検索開始ディレクトリ

解説

grep の --exclude-dir オプションを使用すると、不要なディレクトリを除外して検索できます。
ログ保管用ディレクトリやバックアップディレクトリを対象外にしたい場合に便利です。

grepで除外検索とfindコマンドを組み合わせる方法

ファイル作成

cat << 'EOF' > input.txt error: database connection failed info: application started warning: disk usage high error: timeout occurred info: backup completed EOF

実行コマンド

find . -name "input.txt" -exec grep -v "error" {} \;

実行結果

info: application started
warning: disk usage high
info: backup completed

実行コマンド

find . -name "*.txt" -exec grep -H -v "error" {} \;

実行結果

./input.txt:info: application started
./input.txt:warning: disk usage high
./input.txt:info: backup completed

仕組み

コマンド要素 役割
find . カレントディレクトリ以下を検索
-name "input.txt" 対象ファイル名を指定
-name "*.txt" 拡張子が txt のファイルを指定
-exec 検索したファイルに対してコマンドを実行
grep -v "error" error を含む行を除外して表示(grep 除外)
{} find が見つけたファイル名に置換
\; -exec の終了を示す

解説

grep -v を使うと指定した文字列を含む行を除外できます。

find と組み合わせることで、複数ファイルを対象に効率よく除外検索を実行できます。

grepで除外検索がうまくいかない原因と対処法

ファイル作成

cat << 'EOF' > input.txt INFO: start DEBUG: initialize ERROR: connection failed INFO: retry DEBUG: retry process ERROR: timeout INFO: end EOF

実行コマンド

grep -v "DEBUG" input.txt

実行結果

INFO: start
ERROR: connection failed
INFO: retry
ERROR: timeout
INFO: end

実行コマンド

grep -v DEBUG input.txt

実行結果

INFO: start
ERROR: connection failed
INFO: retry
ERROR: timeout
INFO: end

実行コマンド

grep -v "DEBUG\|ERROR" input.txt

実行結果

INFO: start
INFO: retry
INFO: end

実行コマンド

grep -Ev "DEBUG|ERROR" input.txt

実行結果

INFO: start
INFO: retry
INFO: end

仕組み

原因 内容 対処法
複数条件の指定ミス grep -v "DEBUG|ERROR" grep -Ev "DEBUG|ERROR" または grep -v "DEBUG\|ERROR" を使用する
正規表現の誤り |が特殊文字として扱われない -E を付与するか \| を使用する
大文字小文字の違い debug と DEBUG は別文字列 grep -iv "debug" を使用する
除外対象の確認不足 想定と異なる文字列が含まれている まず grep "対象文字列" で一致内容を確認する

解説

grepの除外検索は -v オプションで実現できますが、複数条件を指定する際は正規表現の扱いが原因で期待通りに動作しないことがよくあります。

-E の利用やパターンの確認を行うことで、多くの問題を解決できます。

grepの除外を理解して効率的に検索

grepの除外機能は、必要な情報だけを素早く抽出するために欠かせない機能です。

-vオプションを中心に、正規表現や複数条件の指定方法を理解することで、検索の精度を大きく向上できます。

また、空行やコメント行の除外、特定のファイルやディレクトリの除外を活用すれば、大量のデータから目的の情報を効率よく見つけられます。

grepの除外を正しく使いこなし、日々の作業をより快適に進めましょう。

コメントを残す

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

©︎ 2025-2026 running terminal commands