はじめに
LinuxやUnix系環境でテキスト処理を行う際、awkとgrepは欠かせないコマンドです。
awkとgrepの基本構文と使い方を理解すると、ログ解析やCSV処理が効率化できます。
本記事では、awkとgrepの基本から実践的な活用方法までを分かりやすく解説します。
awkとgrepの基本構文と使い方
ファイル作成
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
apple 300
grape 120
EOF
実行コマンド
grep "apple" input.txt
実行結果
apple 100
apple 300
実行コマンド
awk '{print $1, $2}' input.txt
実行結果
apple 100
banana 200
orange 150
apple 300
grape 120
実行コマンド
awk '$2 >= 150 {print $1, $2}' input.txt
実行結果
banana 200
orange 150
apple 300
実行コマンド
grep "apple" input.txt | awk '{sum += $2} END {print sum}'
実行結果
400
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | 指定した文字列を含む行を検索する | grep "apple" input.txt |
| awk | 行単位で区切り、列ごとの処理を行う | awk '{print $1, $2}' input.txt |
| awk 条件式 | 指定条件に一致する行のみ抽出する | awk '$2 >= 150 {print $1, $2}' input.txt |
| grep + awk | grepで抽出した結果をawkで集計する | grep "apple" input.txt | awk '{sum += $2} END {print sum}' |
解説
awkは列単位の加工や集計に強く、grepは文字列検索に特化しています。
組み合わせることで、ログ解析やデータ抽出を効率的に行えます。
awkとgrepの使い分け
ファイル作成
cat << 'EOF' > input.txt
apple 120
banana 80
orange 150
grape 90
apple 200
EOF
実行コマンド
grep 'apple' input.txt
実行結果
apple 120
apple 200
実行コマンド
awk '$2 >= 100 { print $1, $2 }' input.txt
実行結果
apple 120
orange 150
apple 200
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | 指定した文字列を含む行を抽出 | grep 'apple' input.txt |
| awk | 列単位で条件判定して加工・抽出 | awk '$2 >= 100 { print $1, $2 }' input.txt |
解説
grepは文字列検索に特化しており、ログ検索などで高速に利用できます。
awkは列処理や条件分岐が可能で、表形式データの加工に適しています。
awkとgrepで正規表現を使う方法
ファイル作成
cat << 'EOF' > input.txt
error: disk full
INFO: process started
warning: cpu high
ERROR: memory leak
user login success
EOF
実行コマンド
grep -E 'error|warning' input.txt
実行結果
error: disk full
warning: cpu high
実行コマンド
grep -Ei 'error|warning' input.txt
実行結果
error: disk full
warning: cpu high
ERROR: memory leak
実行コマンド
awk '/error|warning/' input.txt
実行結果
error: disk full
warning: cpu high
実行コマンド
awk 'tolower($0) ~ /error|warning/' input.txt
実行結果
error: disk full
warning: cpu high
ERROR: memory leak
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep -E | -Eで拡張正規表現を有効化し、|でOR検索 | grep -E 'error|warning' input.txt |
| grep -Ei | -iで大文字小文字を無視して検索 | grep -Ei 'error|warning' input.txt |
| awk | /正規表現/で一致する行を抽出 | awk '/error|warning/' input.txt |
| awk tolower | tolower($0)で全行を小文字化して比較 | awk 'tolower($0) ~ /error|warning/' input.txt |
解説
grepは高速な文字列検索に向いており、ログ監視やフィルタリングでよく利用されます。
awkは正規表現に加えて条件分岐や列処理も可能なため、複雑なテキスト解析に便利です。
awkとgrepで完全一致・部分一致検索を行う方法
ファイル作成
cat << 'EOF' > input.txt
apple
apple pie
banana
banana split
grape
pineapple
EOF
実行コマンド
grep -x "apple" input.txt
実行結果
apple
実行コマンド
grep "apple" input.txt
実行結果
apple
apple pie
pineapple
実行コマンド
awk '$0=="apple"' input.txt
実行結果
apple
実行コマンド
awk '/apple/' input.txt
実行結果
apple
apple pie
pineapple
仕組み
| コマンド | 一致方法 | 実行可能なコマンド | 仕組み |
|---|---|---|---|
| grep | 完全一致 | grep -x "apple" input.txt | -x を使うことで行全体が完全一致した場合のみ表示 |
| grep | 部分一致 | grep "apple" input.txt | 指定文字列を含む行を検索 |
| awk | 完全一致 | awk '$0=="apple"' input.txt | $0 は行全体を表し、== で完全一致判定 |
| awk | 部分一致 | awk '/apple/' input.txt | /文字列/ でパターンに一致する行を検索 |
解説
grep はオプション指定で簡単に完全一致・部分一致を切り替えできます。
awk は条件式を使うことで柔軟な検索や高度なテキスト処理が可能です。
awkとgrepで大文字小文字を区別せず検索する方法
ファイル作成
cat << 'EOF' > input.txt
Apple
apple
Banana
BANANA
Cherry
EOF
実行コマンド
grep -i "apple" input.txt
実行結果
Apple
apple
実行コマンド
awk 'tolower($0) ~ /banana/' input.txt
実行結果
Banana
BANANA
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | -i オプションで大文字小文字を無視して検索 | grep -i "apple" input.txt |
| awk | tolower() で文字列を小文字化して比較 | awk 'tolower($0) ~ /banana/' input.txt |
解説
grepは -i を付与するだけで簡単に大文字小文字を無視できます。
awkは tolower() を利用することで柔軟な条件検索が可能です。
awkとgrepでAND検索・OR検索を行う方法
ファイル作成
cat << 'EOF' > input.txt
apple orange banana
apple grape
banana melon
orange lemon
apple orange
EOF
実行コマンド
grep 'apple' input.txt | grep 'orange'
実行結果
apple orange banana
apple orange
実行コマンド
awk '/apple/ && /orange/' input.txt
実行結果
apple orange banana
apple orange
実行コマンド
grep -E 'apple|orange' input.txt
実行結果
apple orange banana
apple grape
orange lemon
apple orange
実行コマンド
awk '/apple|orange/' input.txt
実行結果
apple orange banana
apple grape
orange lemon
apple orange
仕組み
| 検索方法 | コマンド | 仕組み |
|---|---|---|
| grep AND検索 | grep 'apple' input.txt | grep 'orange' | 1回目のgrep結果を2回目のgrepへ渡して両方含む行を抽出 |
| awk AND検索 | awk '/apple/ && /orange/' input.txt | &&で複数条件を同時に満たす行を抽出 |
| grep OR検索 | grep -E 'apple|orange' input.txt | |でどちらかに一致する行を抽出 |
| awk OR検索 | awk '/apple|orange/' input.txt | 正規表現のOR条件で一致する行を抽出 |
解説
grepはパイプを利用してAND検索を実現し、-EでOR検索を簡潔に記述できます。
awkは&&や|を使うことで、条件式を柔軟に組み合わせて検索できます。
awkとgrepで複数条件を指定する方法
ファイル作成
cat << 'EOF' > input.txt
Alice Tokyo Sales
Bob Osaka Engineering
Carol Tokyo Engineering
Dave Nagoya Sales
Eve Tokyo Marketing
EOF
実行コマンド
grep 'Tokyo' input.txt | grep 'Engineering'
実行結果
Carol Tokyo Engineering
実行コマンド
awk '/Tokyo/ && /Engineering/' input.txt
実行結果
Carol Tokyo Engineering
仕組み
| コマンド | 仕組み |
|---|---|
| grep 'Tokyo' input.txt | grep 'Engineering' | grepをパイプで連結し、「Tokyo」を含み、さらに「Engineering」を含む行だけを抽出 |
| awk '/Tokyo/ && /Engineering/' input.txt | awkで&&を使い、2つの条件に一致する行のみ表示 |
解説
awkでは論理演算子&&を使うことで複数条件を柔軟に組み合わせできます。
条件追加や数値比較も簡単に拡張できます。
awkとgrepで複数キーワードを検索する方法
ファイル作成
cat << 'EOF' > input.txt
Linux awk command
grep is useful
sed and awk together
find with grep
python script
EOF
実行コマンド
grep -E 'awk|grep' input.txt
実行結果
Linux awk command
grep is useful
sed and awk together
find with grep
実行コマンド
awk '/awk|grep/' input.txt
実行結果
Linux awk command
grep is useful
sed and awk together
find with grep
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | -Eで拡張正規表現を有効化し、awk|grep のように | で複数キーワードをOR検索 | grep -E 'awk|grep' input.txt |
| awk | /条件/ の中で awk|grep を使い、OR条件で複数キーワードを検索 | awk '/awk|grep/' input.txt |
解説
grepは高速に複数キーワード検索を行う用途でよく利用されます。
awkは検索だけでなく、列処理や条件分岐も同時に実行できます。
awkとgrepで特定列を抽出する方法
ファイル作成
cat << 'EOF' > input.txt
id,name,department,salary
1,Alice,Sales,5000
2,Bob,Engineering,7000
3,Carol,Marketing,6000
4,David,Engineering,8000
EOF
実行コマンド
awk -F',' '{print $2, $4}' input.txt
実行結果
name salary
Alice 5000
Bob 7000
Carol 6000
David 8000
実行コマンド
grep 'Engineering' input.txt
実行結果
2,Bob,Engineering,7000
4,David,Engineering,8000
実行コマンド
grep 'Engineering' input.txt | awk -F',' '{print $2, $4}'
実行結果
Bob 7000
David 8000
仕組み
| コマンド | 仕組み |
|---|---|
| awk -F',' '{print $2, $4}' input.txt | -F','で区切り文字を,に設定し、$2と$4で2列目と4列目を抽出 |
| grep 'Engineering' input.txt | Engineeringを含む行だけを抽出 |
| grep 'Engineering' input.txt | awk -F',' '{print $2, $4}' | grepで絞り込んだ結果をawkで特定列のみ表示 |
解説
grepは条件に一致する行を検索するコマンドです。
awkを組み合わせることで、必要な列だけを効率的に抽出できます。
awkとgrepでCSVファイルを処理する方法
ファイル作成
cat << 'EOF' > input.txt
id,name,score
1,Alice,85
2,Bob,92
3,Charlie,78
4,David,90
EOF
実行コマンド
grep "Bob" input.txt
実行結果
2,Bob,92
実行コマンド
awk -F',' '{print $2, $3}' input.txt
実行結果
name score
Alice 85
Bob 92
Charlie 78
David 90
実行コマンド
awk -F',' 'NR > 1 && $3 >= 90 {print $2, $3}' input.txt
実行結果
Bob 92
David 90
仕組み
| コマンド | 仕組み |
|---|---|
| grep "Bob" input.txt | grepでCSV内から「Bob」を含む行を検索 |
| awk -F',' '{print $2, $3}' input.txt | awkで,を区切り文字にして2列目(name)と3列目(score)を表示 |
| awk -F',' 'NR > 1 && $3 >= 90 {print $2, $3}' input.txt | NR > 1でヘッダー行を除外し、scoreが90以上の行だけ抽出 |
解説
grepはシンプルな文字列検索に便利です。
awkは条件分岐や列操作が強力で、CSVやログ解析によく利用されます。
awkとgrepで空行や重複行を除外する方法
ファイル作成
cat << 'EOF' > input.txt
apple
orange
apple
banana
orange
grape
banana
EOF
実行コマンド
grep -v '^$' input.txt | awk '!seen[$0]++'
実行結果
apple
orange
banana
grape
仕組み
| コマンド | 役割 | 実行可能なコマンド |
|---|---|---|
| grep -v '^$' | 空行を除外 | grep -v '^$' input.txt |
| awk '!seen[$0]++' | 重複行を除外 | awk '!seen[$0]++' input.txt |
| grep + awk | 空行と重複行を同時に除外 | grep -v '^$' input.txt | awk '!seen[$0]++' |
解説
grepで空行を取り除き、その結果をawkへ渡して重複行を除外しています。
awkの連想配列を利用することで、一度出現した行を記録して再出力を防いでいます。
awkとgrepで行番号付き検索を行う方法
ファイル作成
cat << 'EOF' > input.txt
apple
banana
grape
pineapple
apple juice
orange
EOF
実行コマンド
grep -n "apple" input.txt
実行結果
1:apple
4:pineapple
5:apple juice
実行コマンド
awk '/apple/{print NR ":" $0}' input.txt
実行結果
1:apple
4:pineapple
5:apple juice
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | -n オプションで一致した行番号を表示する | grep -n "apple" input.txt |
| awk | NR 変数で現在行番号を取得して表示する | awk '/apple/{print NR ":" $0}' input.txt |
解説
grep はシンプルな検索に適しており、-n で簡単に行番号を表示できます。
awk は行番号表示に加えて、柔軟な条件分岐や整形処理も可能です。
awkとgrepでファイル内の件数をカウントする方法
ファイル作成
cat << 'EOF' > input.txt
error
success
error
warning
error
success
EOF
実行コマンド
grep -c "error" input.txt
実行結果
3
実行コマンド
awk '/error/ {count++} END {print count}' input.txt
実行結果
3
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | 指定した文字列に一致した行数をカウントする | grep -c "error" input.txt |
| awk | 条件に一致した行ごとに変数を加算して件数を出力する | awk '/error/ {count++} END {print count}' input.txt |
解説
grepはシンプルに件数を確認したい場合に便利です。
awkは条件分岐や集計など複雑な処理にも柔軟に対応できます。
awkとgrepで複数ファイルを横断検索する方法
ファイル作成
cat << 'EOF' > users.log
2026-05-01 INFO user=alice action=login
2026-05-01 ERROR user=bob action=upload
2026-05-02 INFO user=charlie action=logout
EOF
ファイル作成
cat << 'EOF' > system.log
2026-05-01 WARN disk_usage=85%
2026-05-02 ERROR service=db connection_failed
2026-05-03 INFO backup completed
EOF
実行コマンド
grep "ERROR" *.log
実行結果
system.log:2026-05-02 ERROR service=db connection_failed
users.log:2026-05-01 ERROR user=bob action=upload
実行コマンド
awk '/ERROR/ {print FILENAME ":" $0}' *.log
実行結果
system.log:2026-05-02 ERROR service=db connection_failed
users.log:2026-05-01 ERROR user=bob action=upload
実行コマンド
awk '/INFO/ {print FILENAME, $2, $3}' *.log
実行結果
system.log INFO backup
users.log INFO user=alice
users.log INFO user=charlie
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | 複数ファイルを横断して一致文字列を検索 | grep "ERROR" *.log |
| awk | 条件一致した行を加工して表示 | awk '/ERROR/ {print FILENAME ":" $0}' *.log |
| awk | ファイル名や特定列のみ抽出 | awk '/INFO/ {print FILENAME, $2, $3}' *.log |
解説
grepは単純な文字列検索に適しており、複数ファイルを一括検索できます。
awkは検索結果を加工できるため、ログ解析や列抽出に便利です。
awkとgrepでディレクトリ配下を再帰検索する方法
ファイル作成
cat << 'EOF' > input.txt
src/app/main.py:ERROR failed login
src/app/api.py:INFO request success
src/lib/util.py:ERROR timeout
logs/app.log:WARN retry
logs/debug.log:ERROR disk full
EOF
実行コマンド
grep -r "ERROR" .
実行結果
./input.txt:src/app/main.py:ERROR failed login
./input.txt:src/lib/util.py:ERROR timeout
./input.txt:logs/debug.log:ERROR disk full
実行コマンド
awk -F ':' '/ERROR/ {print $1, $2}' input.txt
実行結果
src/app/main.py ERROR failed login
src/lib/util.py ERROR timeout
logs/debug.log ERROR disk full
実行コマンド
grep -r "ERROR" . | awk -F ':' '{print $1, $2}'
実行結果
./input.txt src/app/main.py
./input.txt src/lib/util.py
./input.txt logs/debug.log
仕組み
| 目的 | 実行可能なコマンド | 仕組み |
|---|---|---|
| 再帰検索 | grep -r "ERROR" . | -rでディレクトリ配下を再帰的に検索 |
| 条件抽出 | awk -F ':' '/ERROR/ {print $1, $2}' input.txt | -F ':'で区切り文字を指定し、ERROR行のみ抽出 |
| grepとawk連携 | grep -r "ERROR" . | awk -F ':' '{print $1, $2}' | grep結果をawkへ渡して必要項目のみ表示 |
解説
grepはディレクトリ配下を高速に検索でき、awkは列単位の加工に強みがあります。組み合わせることでログ解析やエラーチェックを効率化できます。
awkとgrepを組み合わせたパイプ処理の実践例
ファイル作成
cat << 'EOF' > input.txt
Alice Sales 120
Bob Marketing 95
Charlie Sales 140
David Engineering 110
Eve Sales 98
Frank Marketing 130
EOF
実行コマンド
awk '$2 == "Sales"' input.txt | grep '[0-9][0-9]$'
実行結果
Alice Sales 120
Charlie Sales 140
Eve Sales 98
仕組み
| 処理内容 | 実行可能なコマンド | 役割 |
|---|---|---|
| Sales部門だけ抽出 | awk '$2 == "Sales"' input.txt | 2列目がSalesの行を抽出 |
| 数値形式の行を絞り込み | grep '[0-9][0-9]$' | 行末が2桁以上の数字の行を検索 |
| パイプ連携 | awk '$2 == "Sales"' input.txt | grep '[0-9][0-9]$' | awkの結果をgrepへ渡して絞り込み |
解説
awkで列単位の条件抽出を行い、grepで文字列パターン検索を追加しています。
パイプ(|)を使うことで、複数コマンドを連携した柔軟なテキスト処理が可能です。
awkとgrepでエラーログを抽出する方法
ファイル作成
cat << 'EOF' > input.txt
2026-05-13 10:00:01 INFO Application started
2026-05-13 10:01:15 ERROR Database connection failed
2026-05-13 10:02:20 WARN Memory usage high
2026-05-13 10:03:45 ERROR Disk space low
2026-05-13 10:04:50 INFO User login success
EOF
実行コマンド
grep "ERROR" input.txt
実行結果
2026-05-13 10:01:15 ERROR Database connection failed
2026-05-13 10:03:45 ERROR Disk space low
実行コマンド
awk '/ERROR/' input.txt
実行結果
2026-05-13 10:01:15 ERROR Database connection failed
2026-05-13 10:03:45 ERROR Disk space low
実行コマンド
awk '/ERROR/ {print $1, $2, $3}' input.txt
実行結果
2026-05-13 10:01:15 ERROR
2026-05-13 10:03:45 ERROR
仕組み
| コマンド | 仕組み | 実行可能なコマンド |
|---|---|---|
| grep | 指定した文字列を含む行を抽出する | grep "ERROR" input.txt |
| awk | パターン一致した行を抽出する | awk '/ERROR/' input.txt |
| awk print | 特定の列だけを表示する | awk '/ERROR/ {print $1, $2, $3}' input.txt |
解説
grepは単純な文字列検索に適しており、ログ解析でよく利用されます。
awkは列単位でデータを加工できるため、詳細なログ分析に便利です。
awkとgrepでアクセスログを集計する方法
ファイル作成
cat << 'EOF' > access.log
192.168.1.10 - - [13/May/2026:10:00:01 +0900] "GET /index.html HTTP/1.1" 200 512
192.168.1.11 - - [13/May/2026:10:00:03 +0900] "POST /login HTTP/1.1" 302 128
192.168.1.10 - - [13/May/2026:10:00:05 +0900] "GET /dashboard HTTP/1.1" 200 1024
192.168.1.12 - - [13/May/2026:10:00:10 +0900] "GET /index.html HTTP/1.1" 404 256
192.168.1.11 - - [13/May/2026:10:00:15 +0900] "GET /dashboard HTTP/1.1" 200 2048
EOF
実行コマンド
grep ' 200 ' access.log
実行結果
192.168.1.10 - - [13/May/2026:10:00:01 +0900] "GET /index.html HTTP/1.1" 200 512
192.168.1.10 - - [13/May/2026:10:00:05 +0900] "GET /dashboard HTTP/1.1" 200 1024
192.168.1.11 - - [13/May/2026:10:00:15 +0900] "GET /dashboard HTTP/1.1" 200 2048
実行コマンド
awk '{print $1}' access.log | sort | uniq -c
実行結果
2 192.168.1.10
2 192.168.1.11
1 192.168.1.12
実行コマンド
grep 'GET' access.log | awk '{print $7}' | sort | uniq -c
実行結果
2 /dashboard
2 /index.html
仕組み
| 目的 | 実行可能なコマンド | 仕組み |
|---|---|---|
| HTTPステータス200のみ抽出 | grep ' 200 ' access.log | grepで正常レスポンスのみを検索 |
| IPごとのアクセス数集計 | awk '{print $1}' access.log | sort | uniq -c | awkでIP抽出後、件数を集計 |
| アクセスURLの集計 | grep 'GET' access.log | awk '{print $7}' | sort | uniq -c | GETリクエストのURLを抽出して集計 |
解説
awkとgrepを組み合わせることで、アクセスログを高速かつ柔軟に集計できます。
シェルのパイプを活用すると、複雑な分析もシンプルなコマンドで実現できます。
awkとgrepで大量データを高速処理する方法
ファイル作成
cat << 'EOF' > input.txt
2026-01-01 INFO login success userA
2026-01-01 ERROR timeout userB
2026-01-01 INFO logout userA
2026-01-01 ERROR disk_full userC
2026-01-01 INFO login success userD
2026-01-01 WARN retry userE
2026-01-01 ERROR timeout userF
EOF
実行コマンド
grep "ERROR" input.txt
実行結果
2026-01-01 ERROR timeout userB
2026-01-01 ERROR disk_full userC
2026-01-01 ERROR timeout userF
実行コマンド
awk '{print $2, $3}' input.txt
実行結果
INFO login
ERROR timeout
INFO logout
ERROR disk_full
INFO login
WARN retry
ERROR timeout
実行コマンド
grep "ERROR" input.txt | awk '{print $3}' | sort | uniq -c
実行結果
1 disk_full
2 timeout
実行コマンド
awk '$2=="ERROR"{count[$3]++} END{for(i in count) print i, count[i]}' input.txt
実行結果
disk_full 1
timeout 2
仕組み
| 仕組み | 実行可能なコマンド | 内容 |
|---|---|---|
| grepで抽出 | grep "ERROR" input.txt | ERROR行のみ高速検索 |
| awkで列抽出 | awk '{print $2, $3}' input.txt | 必要な列だけ取得 |
| grep + awk連携 | grep "ERROR" input.txt | awk '{print $3}' | 抽出後に必要項目だけ加工 |
| awk集計 | awk '$2=="ERROR"{count[$3]++} END{for(i in count) print i, count[i]}' input.txt | 条件別に件数集計 |
| sort + uniq集計 | grep "ERROR" input.txt | awk '{print $3}' | sort | uniq -c | 同一データをカウント |
解説
awkとgrepを組み合わせることで、大量ログから必要な情報を高速に抽出・集計できます。grepで絞り込み、awkで加工する構成にすると処理効率が高くなります。
awkとgrepを効率よく使いこなすためのポイント
awkとgrepは単独でも便利ですが、組み合わせることで強力なテキスト処理環境を構築できます。
grepで必要な行だけを抽出し、その結果をawkで加工・集計する流れは、多くの開発現場で利用されています。
awkとgrepを使いこなせるようになると、ログ解析やサーバー運用、自動化作業の効率が大きく向上します。
基礎を押さえながら、実際のファイル操作で少しずつ慣れていくことが重要です。
![[sed] 実行して理解 指定した範囲の行を削除 文字列のsed](https://running-terminal-commands.com/wp-content/uploads/thumbnail_sed_1920_1080.png)