grepコマンド覚え書き

grep

grepコマンドの細かい使い方について覚え書きをまとめています。

ファイル名と一緒にファイルの中身を表示する

$ ls tmp/*
tmp/20200603071855.txt	tmp/20200603071923.txt	tmp/20200603071951.txt	tmp/20200603072019.txt	tmp/20200603072047.txt	tmp/20200603072115.txt
tmp/20200603071856.txt	tmp/20200603071924.txt	tmp/20200603071952.txt	tmp/20200603072020.txt	tmp/20200603072048.txt	tmp/20200603072116.txt
tmp/20200603071857.txt	tmp/20200603071925.txt	tmp/20200603071953.txt	tmp/20200603072021.txt	tmp/20200603072049.txt	tmp/20200603072117.txt
tmp/20200603071858.txt	tmp/20200603071926.txt	tmp/20200603071954.txt	tmp/20200603072022.txt	tmp/20200603072050.txt	tmp/20200603072118.txt
tmp/20200603071859.txt	tmp/20200603071927.txt	tmp/20200603071955.txt	tmp/20200603072023.txt	tmp/20200603072051.txt	tmp/20200603072119.txt
tmp/20200603071900.txt	tmp/20200603071928.txt	tmp/20200603071956.txt	tmp/20200603072024.txt	tmp/20200603072052.txt	tmp/20200603072120.txt
tmp/20200603071901.txt	tmp/20200603071929.txt	tmp/20200603071957.txt	tmp/20200603072025.txt	tmp/20200603072053.txt	tmp/20200603072121.txt
tmp/20200603071902.txt	tmp/20200603071930.txt	tmp/20200603071958.txt	tmp/20200603072026.txt	tmp/20200603072054.txt	tmp/20200603072122.txt
tmp/20200603071903.txt	tmp/20200603071931.txt	tmp/20200603071959.txt	tmp/20200603072027.txt	tmp/20200603072055.txt	tmp/20200603072123.txt
tmp/20200603071904.txt	tmp/20200603071932.txt	tmp/20200603072000.txt	tmp/20200603072028.txt	tmp/20200603072056.txt	tmp/20200603072124.txt
tmp/20200603071905.txt	tmp/20200603071933.txt	tmp/20200603072001.txt	tmp/20200603072029.txt	tmp/20200603072057.txt	tmp/20200603072125.txt
tmp/20200603071906.txt	tmp/20200603071934.txt	tmp/20200603072002.txt	tmp/20200603072030.txt	tmp/20200603072058.txt	tmp/20200603072126.txt
tmp/20200603071907.txt	tmp/20200603071935.txt	tmp/20200603072003.txt	tmp/20200603072031.txt	tmp/20200603072059.txt	tmp/20200603072127.txt
tmp/20200603071908.txt	tmp/20200603071936.txt	tmp/20200603072004.txt	tmp/20200603072032.txt	tmp/20200603072100.txt	tmp/20200603072128.txt
tmp/20200603071909.txt	tmp/20200603071937.txt	tmp/20200603072005.txt	tmp/20200603072033.txt	tmp/20200603072101.txt	tmp/20200603072129.txt
tmp/20200603071910.txt	tmp/20200603071938.txt	tmp/20200603072006.txt	tmp/20200603072034.txt	tmp/20200603072102.txt	tmp/20200603072130.txt
tmp/20200603071911.txt	tmp/20200603071939.txt	tmp/20200603072007.txt	tmp/20200603072035.txt	tmp/20200603072103.txt	tmp/20200603072131.txt
tmp/20200603071912.txt	tmp/20200603071940.txt	tmp/20200603072008.txt	tmp/20200603072036.txt	tmp/20200603072104.txt	tmp/20200603072132.txt
tmp/20200603071913.txt	tmp/20200603071941.txt	tmp/20200603072009.txt	tmp/20200603072037.txt	tmp/20200603072105.txt	tmp/20200603072133.txt
tmp/20200603071914.txt	tmp/20200603071942.txt	tmp/20200603072010.txt	tmp/20200603072038.txt	tmp/20200603072106.txt	tmp/20200603072134.txt
tmp/20200603071915.txt	tmp/20200603071943.txt	tmp/20200603072011.txt	tmp/20200603072039.txt	tmp/20200603072107.txt	tmp/20200603072135.txt
tmp/20200603071916.txt	tmp/20200603071944.txt	tmp/20200603072012.txt	tmp/20200603072040.txt	tmp/20200603072108.txt	tmp/20200603072136.txt
tmp/20200603071917.txt	tmp/20200603071945.txt	tmp/20200603072013.txt	tmp/20200603072041.txt	tmp/20200603072109.txt	tmp/20200603072137.txt
tmp/20200603071918.txt	tmp/20200603071946.txt	tmp/20200603072014.txt	tmp/20200603072042.txt	tmp/20200603072110.txt
tmp/20200603071919.txt	tmp/20200603071947.txt	tmp/20200603072015.txt	tmp/20200603072043.txt	tmp/20200603072111.txt
tmp/20200603071920.txt	tmp/20200603071948.txt	tmp/20200603072016.txt	tmp/20200603072044.txt	tmp/20200603072112.txt
tmp/20200603071921.txt	tmp/20200603071949.txt	tmp/20200603072017.txt	tmp/20200603072045.txt	tmp/20200603072113.txt
tmp/20200603071922.txt	tmp/20200603071950.txt	tmp/20200603072018.txt	tmp/20200603072046.txt	tmp/20200603072114.txt

上みたいにたくさんファイルがある時にファイルを結合して閲覧したい時があります。catコマンドの引数にファイル名を複数渡すと結合してくれますが、何処までが1ファイルの区切りなのかが分かりません。

そこで一緒にファイル名も確認したい場合のやり方は次の通りです。ついでに行番号も振っています。一ファイル27行なので2ファイル分出力しています。(あまりにも長いので)

$ grep -n "" tmp/* | head -n54
tmp/20200603071855.txt:1:
tmp/20200603071855.txt:2:; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> @10.0.10.123 hoge.com A
tmp/20200603071855.txt:3:; (1 server found)
tmp/20200603071855.txt:4:;; global options: +cmd
tmp/20200603071855.txt:5:;; Got answer:
tmp/20200603071855.txt:6:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22584
tmp/20200603071855.txt:7:;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
tmp/20200603071855.txt:8:
tmp/20200603071855.txt:9:;; OPT PSEUDOSECTION:
tmp/20200603071855.txt:10:; EDNS: version: 0, flags:; udp: 4096
tmp/20200603071855.txt:11:;; QUESTION SECTION:
tmp/20200603071855.txt:12:;hoge.com.			IN	A
tmp/20200603071855.txt:13:
tmp/20200603071855.txt:14:;; ANSWER SECTION:
tmp/20200603071855.txt:15:hoge.com.		86400	IN	A	172.16.0.1
tmp/20200603071855.txt:16:
tmp/20200603071855.txt:17:;; AUTHORITY SECTION:
tmp/20200603071855.txt:18:hoge.com.		86400	IN	NS	ns.hoge.com.
tmp/20200603071855.txt:19:
tmp/20200603071855.txt:20:;; ADDITIONAL SECTION:
tmp/20200603071855.txt:21:ns.hoge.com.		86400	IN	A	172.16.0.2
tmp/20200603071855.txt:22:
tmp/20200603071855.txt:23:;; Query time: 3 msec
tmp/20200603071855.txt:24:;; SERVER: 10.0.10.123#53(10.0.10.123)
tmp/20200603071855.txt:25:;; WHEN: 水  6月 03 07:18:55 UTC 2020
tmp/20200603071855.txt:26:;; MSG SIZE  rcvd: 86
tmp/20200603071855.txt:27:
tmp/20200603071856.txt:1:
tmp/20200603071856.txt:2:; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> @10.0.10.123 hoge.com A
tmp/20200603071856.txt:3:; (1 server found)
tmp/20200603071856.txt:4:;; global options: +cmd
tmp/20200603071856.txt:5:;; Got answer:
tmp/20200603071856.txt:6:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2958
tmp/20200603071856.txt:7:;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
tmp/20200603071856.txt:8:
tmp/20200603071856.txt:9:;; OPT PSEUDOSECTION:
tmp/20200603071856.txt:10:; EDNS: version: 0, flags:; udp: 4096
tmp/20200603071856.txt:11:;; QUESTION SECTION:
tmp/20200603071856.txt:12:;hoge.com.			IN	A
tmp/20200603071856.txt:13:
tmp/20200603071856.txt:14:;; ANSWER SECTION:
tmp/20200603071856.txt:15:hoge.com.		86400	IN	A	172.16.0.1
tmp/20200603071856.txt:16:
tmp/20200603071856.txt:17:;; AUTHORITY SECTION:
tmp/20200603071856.txt:18:hoge.com.		86400	IN	NS	ns.hoge.com.
tmp/20200603071856.txt:19:
tmp/20200603071856.txt:20:;; ADDITIONAL SECTION:
tmp/20200603071856.txt:21:ns.hoge.com.		86400	IN	A	172.16.0.2
tmp/20200603071856.txt:22:
tmp/20200603071856.txt:23:;; Query time: 3 msec
tmp/20200603071856.txt:24:;; SERVER: 10.0.10.123#53(10.0.10.123)
tmp/20200603071856.txt:25:;; WHEN: 水  6月 03 07:18:56 UTC 2020
tmp/20200603071856.txt:26:;; MSG SIZE  rcvd: 86
tmp/20200603071856.txt:27:

これでファイルを結合しながら閲覧しやすくなります。

grepでマッチした行から指定行数まで抽出する

例えば先程の標準出力の内「DiG」を含む行から2行分まで出力したい時などのやり方です。

$ grep -n "" tmp/* | head -n54 | grep "DiG" -A2
tmp/20200603071855.txt:2:; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> @10.0.10.123 hoge.com A
tmp/20200603071855.txt:3:; (1 server found)
tmp/20200603071855.txt:4:;; global options: +cmd
--
tmp/20200603071856.txt:2:; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> @10.0.10.123 hoge.com A
tmp/20200603071856.txt:3:; (1 server found)
tmp/20200603071856.txt:4:;; global options: +cmd
ChannoMBP:Lines mt.date$ 

-Bオプションで前も抽出できます。

コメントを残す

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

CAPTCHA