6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gzファイルにgrepかけよう!

Last updated at Posted at 2019-12-12

UNIX ライクなシステムにおいては, gzip (gz) で圧縮されたファイルを目にすることが多いです.
それらのファイルに検索したいときと思ったとき, どうするか.

zgrep コマンドを使います.
たいていのUNIX/Linuxディストリビューションにおいて, 標準で使える機能です.

$ zgrep -nH "KEYWORD" FILENAME

ところで, いくつものファイルがgz圧縮されていることがあります.
ログファイルなど.

ふつうのテキストファイルが複数あるような場合には, grepコマンドを用いた検索,
grep -rnHI "KEYWORD" DIRECTORY で対応できます.

しかし, zgrepファイルには, -r オプション (再帰探索?) がない.

どうするか.

find/xargs コマンドを組み合わせて解決しましょう.

$ find DIRECTORY -type f -name "*.gz" -print0 | xargs -0 zgrep -nH "KEYWORD"

ここでは理由を割愛しますが,
findコマンドの -print0 と xargsコマンドの -0 はあってもなくてもいいですが,
つけておいたほうが無難です.

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?