はじめに
grepはテキスト検索で欠かせないコマンドですが、行末を条件に検索する方法で迷う初学者は少なくありません。
行末指定を理解すると、特定の拡張子やログの末尾パターンなどを効率よく抽出できます。
本記事では、基本から応用まで詳しく解説します
参考:GNU grep
grepで行末を指定する基本構文
ファイル作成
cat << 'EOF' > input.txt
apple
banana
orange
grape
melon
apple pie
green apple
banana
orange juice
pineapple
EOF
実行コマンド
grep 'apple$' input.txt
実行結果
apple
green apple
pineapple
実行コマンド
grep 'juice$' input.txt
実行結果
orange juice
実行コマンド
grep 'banana$' input.txt
実行結果
banana
banana
仕組み
| 項目 | 内容 |
|---|---|
| $ | 行末を表すアンカー(メタ文字) |
| grep '文字列$' | 指定した文字列で終わる行のみを検索する |
| 部分一致との違い | 行の途中ではなく、末尾が一致した場合のみヒットする |
| 使用例 | 拡張子の検索や特定の接尾辞を持つ行の抽出などで利用される |
解説
grepでは$を使用すると行末を指定できます。
行の最後が指定した文字列で終わるデータだけを抽出したい場合に便利です。
grepで特定の文字列で行末が終わる行を検索する
ファイル作成
cat << 'EOF' > input.txt
apple
banana
grape
orange
pineapple
green apple
red apple
EOF
実行コマンド
grep 'apple$' input.txt
実行結果
apple
pineapple
green apple
red apple
仕組み
| 項目 | 内容 |
|---|---|
| コマンド | grep 'apple$' input.txt |
| apple | 検索したい文字列を指定する。 |
| $ | 正規表現で「行末」を表し、appleで終わる行だけを検索する。 |
| input.txt | 検索対象のファイル。 |
解説
grepでは正規表現を利用でき、$を付けることで行末に一致する文字列だけを検索できます。
行末一致を利用すると、特定の接尾辞を持つ行だけを効率よく抽出できます。
grepで行末と行頭を組み合わせて完全一致検索する
ファイル作成
cat << 'EOF' > input.txt
apple
Apple
apple pie
pineapple
apple
EOF
実行コマンド
grep '^apple$' input.txt
実行結果
apple
apple
仕組み
| パターン | 意味 |
|---|---|
| ^ | 行頭を表す |
| apple | 検索したい文字列 |
| $ | 行末を表す |
| ^apple$ | 行頭から行末までが apple と完全に一致する行だけを検索する |
解説
grep '^apple$' のように行頭(^)と行末($)を組み合わせることで、文字列全体が一致する行だけを検索できます。
apple pie や pineapple のように文字列を含むだけの行は一致しません。
grepで複数の行末パターンを検索する方法
ファイル作成
cat << 'EOF' > input.txt
error
warning
info
error.log
warning.log
info.log
server-error
server-warning
server-info
backup-error
backup-warning
backup-info
EOF
実行コマンド
grep -E '(error|warning)$' input.txt
実行結果
error
warning
server-error
server-warning
backup-error
backup-warning
仕組み
| 項目 | 内容 |
|---|---|
| grep | テキストから条件に一致する行を検索するコマンド |
| -E | 拡張正規表現を有効にし、|を使用できるようにする |
| (error|warning) | error または warning のどちらかに一致する |
| $ | 行末を表し、指定した文字列で終わる行のみを検索する |
解説
複数の行末パターンは、拡張正規表現とOR演算子(|)を組み合わせることで1回のgrepで検索できます。
$を付けることで、指定した文字列が行末にある場合だけ一致します。
grepで行末が数字・英字・記号の行を検索する
ファイル作成
cat << 'EOF' > input.txt
apple
banana123
orange!
grape?
melon.
peach-
kiwi_
lemon9
cat@
dog#
bird$
fish%
EOF
実行コマンド
grep '[0-9]$' input.txt
実行結果
banana123
lemon9
実行コマンド
grep '[[:alpha:]]$' input.txt
実行結果
apple
実行コマンド
grep '[[:punct:]]$' input.txt
実行結果
orange!
grape?
melon.
peach-
kiwi_
cat@
dog#
bird$
fish%
仕組み
| 項目 | 内容 |
|---|---|
| $ | 行末を表すアンカー |
| [0-9] | 0~9の数字1文字に一致 |
| [[:alpha:]] | 英字(A~Z、a~z)1文字に一致 |
| [[:punct:]] | 記号(! @ # $ % . ? - など)1文字に一致 |
| grep '[パターン]$' | 行末が指定した文字種の行のみを検索する |
解説
$は行末を表す正規表現のアンカーです。
文字クラスと組み合わせることで、行末が数字・英字・記号の行だけを簡単に抽出できます。
grepで空白を含む行末を検索する方法
ファイル作成
cat << 'EOF' > input.txt
normal line
space at end
tab at end
multiple spaces at end
no trailing whitespace
EOF
実行コマンド
grep -nE '[[:blank:]]+$' input.txt
実行結果
2:space at end
3:tab at end
4:multiple spaces at end
仕組み
| パターン | 意味 |
|---|---|
| grep | テキストを検索するコマンド |
| -n | 一致した行番号を表示する |
| -E | 拡張正規表現を使用する |
| [[:blank:]] | 半角スペースまたはタブに一致する |
| + | 直前の文字が1回以上続くことを表す |
| $ | 行末を表すため、行末の空白のみを検索できる |
解説
[[:blank:]]+$ を使用すると、行末にあるスペースやタブだけを検出できます。
不要な行末の空白を確認する際によく利用される grep の正規表現です。
grepで空行を検索する方法
ファイル作成
cat << 'EOF' > input.txt
apple
banana
grape
orange
melon
EOF
実行コマンド
grep '$' input.txt
実行結果
apple
banana
grape
orange
melon
実行コマンド
grep -n '^$' input.txt
実行結果
2:
5:
8:
仕組み
| パターン | 意味 | 用途 |
|---|---|---|
| $ | 行末にマッチする | すべての行に一致する(空行も含む) |
| ^ | 行頭にマッチする | 行の先頭を表す |
| ^$ | 行頭の直後に行末がある | 文字が1文字もない空行のみを検索する |
| -n | 行番号を表示する | 空行がある行番号を確認できる |
解説
grepで空行を検索するには、行頭(^)と行末($)が連続する^$を使用します。
-nオプションを付けることで、空行そのものだけでなく、空行が存在する行番号も確認できます。
grepで-Eオプションを使った行末検索
ファイル作成
cat << 'EOF' > input.txt
apple
banana
orange
grape
apple.txt
banana.txt
orange.log
grape.csv
README
EOF
実行コマンド
grep -E '.txt$' input.txt
実行結果
apple.txt
banana.txt
実行コマンド
grep -E 'e$' input.txt
実行結果
apple
orange
grape
仕組み
| 項目 | 説明 |
|---|---|
| grep | 条件に一致する行を検索するコマンド |
| -E | 拡張正規表現(Extended Regular Expression)を有効にするオプション |
| $ | 行末を表すメタ文字 |
| \.txt$ | .txtで行が終わる行を検索する(.は\.でエスケープ) |
| e$ | 行末がeで終わる行を検索する |
解説
grep -Eでは拡張正規表現を利用でき、$を使うことで行末に一致する文字列だけを検索できます。
ファイル名の拡張子や特定の語尾を持つ行を抽出する際によく利用されます。
grepで複数ファイルから行末を検索する方法
ファイル作成
cat << 'EOF' > file1.txt
apple
banana
orange
grape
EOF
ファイル作成
cat << 'EOF' > file2.txt
melon
banana
pineapple
orange
EOF
ファイル作成
cat << 'EOF' > file3.txt
strawberry
apple
kiwi
banana
EOF
実行コマンド
grep 'banana$' file1.txt file2.txt file3.txt
実行結果
file1.txt:banana
file2.txt:banana
file3.txt:banana
実行コマンド
grep 'orange$' file1.txt file2.txt file3.txt
実行結果
file1.txt:orange
file2.txt:orange
実行コマンド
grep -H 'apple$' file1.txt file2.txt file3.txt
実行結果
file1.txt:apple
file2.txt:pineapple
file3.txt:apple
仕組み
| 項目 | 内容 |
|---|---|
| grep | 指定したパターンを検索するコマンド |
| banana$ | $は行末を表すため、行末がbananaで終わる行だけ一致する |
| file1.txt file2.txt file3.txt | 複数ファイルをまとめて検索する |
| -H | 一致した行の前にファイル名を必ず表示する |
解説
grepでは検索文字列の末尾に$を付けることで、行末に一致する文字列のみ検索できます。
複数ファイルを指定すると、一度の実行ですべてのファイルから条件に一致する行を検索できます。
grepで再帰検索時に行末条件を指定する方法
ファイル作成
mkdir -p sample/{dir1,dir2}
ファイル作成
cat << 'EOF' > sample/dir1/file1.txt
error
error log
warning
EOF
ファイル作成
cat << 'EOF' > sample/dir2/file2.txt
error
fatal error
error.log
EOF
実行コマンド
grep -r 'error$' sample
実行結果
sample/dir2/file2.txt:error
sample/dir2/file2.txt:fatal error
sample/dir1/file1.txt:error
仕組み
| 項目 | 内容 |
|---|---|
| grep | テキストを検索するコマンド |
| -r | ディレクトリを再帰的に検索する |
| error$ | $は行末アンカーを表し、「error」で行が終わる行だけ一致する |
| 一致しない例 | error log、error.log は行末が error ではないため一致しない |
解説
grepでは正規表現の$を使うことで、行末を条件に検索できます。
-rと組み合わせることで、配下のすべてのファイルから行末条件に一致する行を効率よく検索できます。
grepでログファイルの行末を検索する実践例
ファイル作成
cat << 'EOF' > input.txt
2025-01-10 10:00:01 INFO Application started
2025-01-10 10:01:15 ERROR Database connection failed
2025-01-10 10:02:30 INFO User login success
2025-01-10 10:03:45 ERROR Disk full
2025-01-10 10:04:50 INFO Backup completed
EOF
実行コマンド
grep 'failed$' input.txt
実行結果
2025-01-10 10:01:15 ERROR Database connection failed
実行コマンド
grep 'full$' input.txt
実行結果
2025-01-10 10:03:45 ERROR Disk full
実行コマンド
grep 'success$' input.txt
実行結果
2025-01-10 10:02:30 INFO User login success
仕組み
| 項目 | 内容 |
|---|---|
| grep | 指定したパターンに一致する行を検索するコマンド |
| $ | 正規表現で「行末」を表すメタ文字 |
| failed$ | 行末が failed の行だけを検索する |
| full$ | 行末が full の行だけを検索する |
| success$ | 行末が success の行だけを検索する |
解説
grepでは正規表現の$を使用すると、文字列が行末にある場合だけ一致します。
ログファイルの末尾にあるステータスやエラーメッセージを抽出したい場合に便利です。
grepで行末検索がうまくいかない原因と対処法
ファイル作成
cat << 'EOF' > input.txt
apple
apple
banana
orange
apple$
EOF
実行コマンド
grep "apple$" input.txt
実行結果
apple
実行コマンド
grep -E "apple$" input.txt
実行結果
apple
実行コマンド
grep '$$' input.txt
実行結果
apple$
実行コマンド
grep -n "apple " input.txt
実行結果
2:apple
仕組み
| 内容 | コマンド | 動作 |
|---|---|---|
| 行末指定 | $ | パターンが行の最後に存在する場合だけ一致 |
| 文字としての $ | \$$ | $ をエスケープして検索 |
| 基本正規表現 | grep | $ などの正規表現記号を解釈 |
| 拡張正規表現 | grep -E | より多くの正規表現機能を利用可能 |
| 末尾空白 | grep "apple " | 見えない空白文字も一致条件になる |
解説
grep 行末 の検索で期待通りにならない主な原因は、$ が「行末」を表す正規表現記号として解釈されるためです。
また、末尾に空白や特殊文字が存在すると見た目と実際の文字列が異なり、検索結果がずれることがあります。
grepで行末検索時によく使うオプション一覧
ファイル作成
cat << 'EOF' > input.txt
error
error
error.log
test error
EOF
実行コマンド
grep 'error$' input.txt
実行結果
error
test error
実行コマンド
grep -n 'error$' input.txt
実行結果
1:error
4:test error
実行コマンド
grep -E 'error$' input.txt
実行結果
error
test error
実行コマンド
grep -i 'ERROR$' input.txt
実行結果
error
test error
実行コマンド
grep -v 'error$' input.txt
実行結果
error
error.log
仕組み
| コマンド・オプション | 役割 | 行末検索での仕組み |
|---|---|---|
| grep '文字$' | 行末一致検索 | $ が文字列の終端位置を指定する |
| -n | 行番号表示 | 一致した行の位置を確認できる |
| -E | 拡張正規表現 | 複雑な正規表現と組み合わせやすい |
| -i | 大文字小文字無視 | ERROR$ と error$ を同じ扱いにする |
| -v | 反転検索 | 行末条件に一致しない行を取得する |
解説
grep の行末検索では正規表現の $ を使用します。
$ は「行の最後」を意味するため、ログ解析や設定ファイル検索で特定文字で終わる行を抽出するときによく利用されます。
grepの行末指定ポイントまとめ
grepの行末検索は、正規表現の基本を押さえるだけで幅広い場面に応用できます。
基本構文を理解したうえで、完全一致や複数条件、-Eオプション、複数ファイル検索などを使い分ければ、作業効率を大きく向上できます。
今回紹介した内容を身に付ければ、さまざまなテキストやログ解析でも自信を持って活用できるようになります。
