一、 grep
基本的な使用法
1. ファイル内の文字列を検索する
📄構文形式:grep "検索対象の文字列" /対象ファイルパス/検索ファイル名
[root@hcss-ecs-72ce H100]# cat ./test.txt
hhhhhh
name
[root@hcss-ecs-72ce H100]# grep "name" ./test.txt
name
[root@hcss-ecs-72ce H100]#
📢:./ は現在のディレクトリを意味します
2. 現在のディレクトリとサブディレクトリ内で文字列を再帰的に検索する
[root@hcss-ecs-72ce home]# grep -r "name" ./
./H100/test.txt:name
./H100/test.t:name
オプションと修飾子
1. 大小文字を区別しない(-i)
検索時に大文字と小文字を区別しない
[root@hcss-ecs-72ce home]# grep -i "name" ./H100/test.txt
name
Name
[root@hcss-ecs-72ce home]#
2. 行番号を表示する(-n)
一致した行とその行番号を出力します
[root@hcss-ecs-72ce home]# grep -n "name" ./H100/test.txt
1:hhhhhh,my name is paipai
2:name
[root@hcss-ecs-72ce home]#
3. ファイル名のみを表示する(-l)
一致する文字列を含むファイル名のみを出力し、具体的な一致内容は表示しません
[root@hcss-ecs-72ce H100]# grep -l "name" *
test.t
test.txt
[root@hcss-ecs-72ce H100]#
📢:最初にコマンドを実行した際には何も表示されなかったため、コマンドが間違っていると誤解しました。最終的に文字列後の引用符が全角文字だったことが原因であることに気づきました😭~ ⚠️:Linuxでは、コマンドで全角記号は認識されません。最後に確認してみる必要があることを忘れないようにしてください。
4. 逆方向マッチング(-v)
一致する文字列を含まない行を出力します
[root@hcss-ecs-72ce H100]# cat test.log
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]# grep -v "name" ./test.log
hhhhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]#
5. 再帰検索(-r または -R)
指定されたディレクトリとそのサブディレクトリ内のすべてのファイルを検索します
[root@hcss-ecs-72ce H100]# grep -r "name" /home
/home/H101/ok.txt:you name balala
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]# grep -R "name" /home
/home/H101/ok.txt:you name balala
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]#
6. 正規表現(-E)
正規表現を使用して検索します。例えば、ファイル内の数字0-9を検索します
[root@hcss-ecs-72ce H100]# cat test.log
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
1
2
3
8
9
[root@hcss-ecs-72ce H100]# grep -E [0-9] ./test.log
1
2
3
8
9
[root@hcss-ecs-72ce H100]#
7. カウント(-c)
一致した文字列を出力せず、一致した行数を出力します
[root@hcss-ecs-72ce H100]# cat test.log
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]# grep -c "name" ./test.log
3
[root@hcss-ecs-72ce H100]#
8. 前後文の表示(-B -A -C)
| 文字 | 意味 |
|---|---|
| -B n | before、一致した行の前のn行を表示 |
| -A n | after、一致した行の後のn行を表示 |
| -C n | counts、一致した行の前後n行を表示 |
[root@hcss-ecs-72ce H100]# grep -B 2 "wow" ./test.log
my name is peiqi
my name is hhhh
wow
[root@hcss-ecs-72ce H100]# grep -A 2 "wow" ./test.log
wow
come here
baby car
[root@hcss-ecs-72ce H100]# grep -C 2 "wow" ./test.log
my name is peiqi
my name is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce H100]#
⚠️ -c と -C の違いに注意してください。混同しないようにしてください
9. 一致したバイトオフセットを表示する(-b)
一致したバイトオフセットを表示します ❓バイトオフセット—ファイルの先頭から特定データノードまでの文字数
[root@hcss-ecs-72ce H100]# grep -b "wow" ./test.log
58:wow
[root@hcss-ecs-72ce H100]#
💽 隠し機能 オプションbの後にnを追加すると、対象文字の上n行の先頭文字に対応するバイトオフセットを見ることができます
[root@hcss-ecs-72ce H100]# grep -b3 "wow" ./test.log
7-my name is paipai
25-my name is peiqi
42-my name is hhhh
58:wow
62-come here
72-baby car
81-bye~
[root@hcss-ecs-72ce H100]#
パイプと他のコマンドの組み合わせ
grepは他のコマンドと組み合わせて、出力されたテキストをフィルタリングして検索するためによく使用されます
[root@hcss-ecs-72ce H100]# cat ./test.log |grep "name"
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce H100]#
一般的なgrepの使用例
1. 特定のファイルで文字列を検索する(2つの方法)
方法1:標準的な実装
[root@hcss-ecs-72ce H100]# grep "name" ./test.log
my name is paipai
my name is peiqi
my name is hhhh
方法2:パイプを使ったフィルタリング
[root@hcss-ecs-72ce H100]# cat ./test.log |grep "name"
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce H100]#
2. カーネルメッセージから「usb」を大文字小文字を無視して検索する
[root@hcss-ecs-72ce H100]# dmesg |grep -i "usb"
結果が多すぎるので図を貼ります
3. 指定ディレクトリ内で再帰的に検索する
[root@hcss-ecs-72ce H100]# grep -i -r "name" /home/
/home/H101/ok.txt:you name balala
/home/H101/ok.txt:NAME
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]#
4. 現在のディレクトリ内の各「XXXX」の出現行と行番号を再帰的に検索する
[root@hcss-ecs-72ce H100]# grep -n -r "name" /home/
/home/H101/ok.txt:1:you name balala
/home/H100/test.log:2:my name is paipai
/home/H100/test.log:3:my name is peiqi
/home/H100/test.log:4:my name is hhhh
[root@hcss-ecs-72ce H100]#
二、 sed
強力なテキスト処理ツールであり、ファイル内の文字列を変更・置換するのに適しており、エディタを開かずにファイルの内容を編集できます。以下はsedの基本コマンドのまとめです
1. 🔭 検索
行番号による検索
- 単一行の検索(📌sedの検索原理により、パラメータ-nを指定しないと検索の過程全体が表示される)
[root@hcss-ecs-72ce ~]# sed -n '3p' /home/H100/test.log
my name is peiqi
[root@hcss-ecs-72ce ~]# sed '3p' /home/H100/test.log
hhhhhh
my name is paipai
my name is peiqi
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
1
2
3
8
9
[root@hcss-ecs-72ce ~]#
- 非連続の複数行の検索はセミコロンで区切る
[root@hcss-ecs-72ce ~]# sed -n '1p;4p' /home/H100/test.log
hhhhhh
my name is hhhh
[root@hcss-ecs-72ce ~]#
- 連続する複数行の検索はカンマで区切り、pは省略する
[root@hcss-ecs-72ce ~]# sed -n '1,4p' /home/H100/test.log
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce ~]#
ファイル内容による検索
- 単一の内容を検索 📌 単一の内容を検索しても、行全体が表示される
[root@hcss-ecs-72ce ~]# sed -n '/paipai/p' /home/H100/test.log
my name is paipai
[root@hcss-ecs-72ce ~]#
- 複数の連続する内容を検索
[root@hcss-ecs-72ce ~]# sed -n '/paipai/,/car/p' /home/H100/test.log
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce ~]#
- 複数の非連続な内容を検索 📌 非連続な内容を検索する際は、それぞれの内容の後ろにpを追加する必要があります
[root@hcss-ecs-72ce ~]# sed -n '/paipai/;/car/p' /home/H100/test.log
sed:-e 表達式 #1,文字 9:不明なコマンド:";"
[root@hcss-ecs-72ce ~]# sed -n '/paipai/p;/car/p' /home/H100/test.log
my name is paipai
baby car
[root@hcss-ecs-72ce ~]#
2. 置換(-i)
📌 構文形式 sed -i "s#置換対象#置換後の文字列#g" 対象ファイルパス
[root@hcss-ecs-72ce ~]# cat /home/H100/test.log
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce ~]# sed -i "s#name#hello#g" /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat /home/H100/test.log
hhhhhh
my hello is paipai
my hello is peiqi
my hello is hhhh
3. 削除(d)
- 単一行の削除 ⚠️ 削除コマンドを実行する際、実際にファイルから削除するには-iパラメータを追加する必要があります。それ以外は表示のみの削除になります
[root@hcss-ecs-72ce ~]# sed '11d' /home/H100/test.log
hhhhhh
my hello is paipai
my hello is hhhh
wow
come here
baby car
bye~
1
2
3
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 hhhhhh
2 my hello is paipai
3 my hello is hhhh
4 wow
5 come here
6 baby car
7 bye~
8 1
9 2
10 3
11 8
[root@hcss-ecs-72ce ~]# sed -i '11d' /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 hhhhhh
2 my hello is paipai
3 my hello is hhhh
4 wow
5 come here
6 baby car
7 bye~
8 1
9 2
10 3
[root@hcss-ecs-72ce ~]#
- 複数非連続行の削除
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 hhhhhh
2 my hello is paipai
3 my hello is hhhh
4 wow
5 come here
6 baby car
7 bye~
8 1
9 2
10 3
[root@hcss-ecs-72ce ~]# sed -i '1d;10d' /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 my hello is hhhh
3 wow
4 come here
5 baby car
6 bye~
7 1
8 2
[root@hcss-ecs-72ce ~]#
- 連続する複数行の削除
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 my hello is hhhh
3 wow
4 come here
5 baby car
6 bye~
7 1
8 2
[root@hcss-ecs-72ce ~]# sed -i '6,8d' /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 my hello is hhhh
3 wow
4 come here
5 baby car
[root@hcss-ecs-72ce ~]#
4. 追加(挿入と追加)
⚠️ パラメータは前に-を付けず、直接行番号を指定します
| パラメータ | 追加場所 |
|---|---|
| a | after、指定行の後に新しい行を追加 |
| i | initial、指定行の前に新しい行を追加 |
- 指定行の後に新しい行を追加します。メモリに書き込む場合は-iパラメータが必要です
[root@hcss-ecs-72ce ~]# sed '1a here is a new line of text' /home/H100/test.log
my hello is paipai
here is a new line of text
my hello is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 my hello is hhhh
3 wow
4 come here
5 baby car
[root@hcss-ecs-72ce ~]# sed -i '1a here is a new line of text' /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 here is a new line of text
3 my hello is hhhh
4 wow
5 come here
6 baby car
[root@hcss-ecs-72ce ~]#
- 指定行の前に新しい行を追加します。直接ディスクに書き込みます
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 here is a new line of text
3 my hello is hhhh
4 wow
5 come here
6 baby car
[root@hcss-ecs-72ce ~]# sed -i '4i we are friends' /home/H100/test.log
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log
1 my hello is paipai
2 here is a new line of text
3 my hello is hhhh
4 we are friends
5 wow
6 come here
7 baby car
[root@hcss-ecs-72ce ~]#
三、 awk
配列を含む複雑なデータ構造やカスタム関数をサポートし、複雑なデータ処理プログラムを作成できます。 awkはプログラミング言語であり、以下は基本的な特徴です。さらに高度な機能(ループ、条件文、ユーザー定義関数など)もサポートしています。今後の学習の可能性があります
情報の取得
awkを使ってファイルの内容を取得します
[root@hcss-ecs-72ce ~]# awk '{print}' /home/paipai/awk_practice.txt
00 ben 18 60
01 ken 23 66
02 pen 14 30
03 kei 43 70
行番号による検索
- 単一行の情報を取得
[root@hcss-ecs-72ce ~]# awk 'NR==2' /home/paipai/awk_practice.txt
00 ben 18 60
- 非連続の複数行情報を取得
[root@hcss-ecs-72ce ~]# awk 'NR==1;NR==4' /home/paipai/awk_practice.txt
编号 姓名 年龄 体重
02 pen 14 30
- 連続する複数行情報を取得
[root@hcss-ecs-72ce ~]# awk 'NR==1,NR==5' /home/paipai/awk_practice.txt
编号 姓名 年龄 体重
00 ben 18 60
01 ken 23 66
02 pen 14 30
03 kei 43 70
[root@hcss-ecs-72ce ~]#
情報の置換(変更)
まず行情報を取得し、変更する情報を指定し、表示列を指定します 📌 全体的な構造
awk '/xxx/{gsub(/変更する文字列/,"変更後の文字列","$n変更する列");print$n}' 操作対象ファイル
🔆 コマンドが複雑な場合は2つの部分に分けて考えるとよい メイン構造:
awk '/検索する文字列/{print $出力する列番号}' 操作対象ファイル
出力内容の変更:
gsub(/変更する文字列/,"変更後の文字列",$変更する列番号);
[root@hcss-ecs-72ce ~]# awk '/ben/{gsub(/:/,"%",$NF);print}' /home/paipai/awk_practice.txt
ben%18%60%yunnan
[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
[root@hcss-ecs-72ce ~]#
情報の除外
例えば、空行と#で始まる行を除外します
[root@hcss-ecs-72ce ~]# awk "{print}" /home/paipai/awk_practice.txt
编号 姓名 年龄 体重
00 ben 18 60
01 ken 23 66
02 pen 14 30
03 kei 43 70
#05 lily 19 50
06 baby 23 69
08 zooc 32 45
[root@hcss-ecs-72ce ~]# awk '!/^#|^$/' /home/paipai/awk_practice.txt
编号 姓名 年龄 体重
00 ben 18 60
01 ken 23 66
02 pen 14 30
03 kei 43 70
06 baby 23 69
08 zooc 32 45
[root@hcss-ecs-72ce ~]#
三つのツールの共通点
1. ファイルのフィルタリングと選択が可能
- sedはフィルタリング操作を行う際に-nパラメータを追加する必要があります(sedの動作原理による)
- sed、awkの検索文字列は/ xxxx/で囲む必要があります。sedは検索文字列の後にpパラメータを追加する必要があります。一方、awkはpを追加しないと正常に動作しません
☀️ grep
[root@hcss-ecs-72ce ~]# grep "dew" /home/paipai/awk_practice.txt
dew:19:65:beijing
[root@hcss-ecs-72ce ~]#
☀️ sed
[root@hcss-ecs-72ce ~]# sed -n "/dew/p" /home/paipai/awk_practice.txt
dew:19:65:beijing
[root@hcss-ecs-72ce ~]#
☀️ awk
[root@hcss-ecs-72ce ~]# awk "/dew/" /home/paipai/awk_practice.txt
dew:19:65:beijing
[root@hcss-ecs-72ce ~]#
空行の表示を除外
通常、空行の表示を除外する際には正規表現と一緒に使用する必要があります
☀️ grep
grep -E と egrep は同じ機能を持ち、正規表現を認識できるようにします。 grepの-vは反転を意味します。 以下のように2つの方法があります
[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# egrep -v "^$" /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# grep -E -v "^$" /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
☀️ sed
注意:sedコマンドを使用してフィルタリングする際は、sedの後に-nパラメータを必ず追加してください。さもないと、sedの動作原理により、検索の全過程が表示されてしまいます(つまり、元の内容が表示されます) sedの検索では単引号を使用します
[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# sed -n '/^$/!p' /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]#
☀️ awk
awkはsedとは異なり、反転を示す「!」は文字列の前に配置する必要があり、pを追加すると結果が表示されません。これは何度も強調されています
[root@hcss-ecs-72ce ~]# awk '!/^$/' /home/paipai/awk_practice.txt
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# awk '/^$/!' /home/paipai/awk_practice.txt
awk: cmd. line:2: /^$/!
awk: cmd. line:2: ^ unexpected newline or end of string
[root@hcss-ecs-72ce ~]#
三つのツールを使用する際の注意事項
1. 対象文字列の操作
❓対象文字列にダブルクォートとシングルクォートのどちらも使用できますか
| 記号 | 違い |
|---|---|
| grep | ダブルクォート、シングルクォートの両方を使用可能 |
| sed | 一部のコマンドでは両方を使用可能だが、反転コマンドはシングルクォートを使用する必要がある |
| awk | シングルクォートのみ |
📢:覚えていない場合はすべての場面でシングルクォートを使用することをお勧めします
2. 出力文字列にpを追加
sedコマンドでは、出力文字列の' /xxxxxx/p'の部分にpを追加する必要があります。grepとawkは必要ありません。 具体的な原理は以下の通りです
| 記号 | 動作原理 |
|---|---|
| sed | pを追加しないと、検索文がエラーになる。また、デフォルトでは検索の全過程を表示するため、-nパラメータを追加して表示を解除する必要がある |
| awk | pを追加すると、検索の全過程を表示してしまう |