Windows版のripgrepで圧縮ファイルを検索する方法

[PR]記事内のアフィリエイトリンクから収入を得る場合があります
  • URLをコピーしました!

ripgrep」はテキストを高速に検索することができるツールで、macOS/Linux/Windowsなどさまざまなプラットフォームで動作します。

ripgrep(rg)は、単にテキストファイルを検索することができるだけでなく「-z(--search-zip)」オプションを指定すると、圧縮ファイルの中身のテキストファイルを検索することもできるのですが、このオプションが何故かWindowsでは期待通りに動作しませんでした(macOSでは問題なく動作していました)

以下、Windows版のripgrepで圧縮ファイル(今回は.gzファイル)を検索する方法を説明します。

目次

Windows版ripgrepで圧縮ファイルを検索

結論から書くと、ripgrepで圧縮ファイル検索(-z)を有効にするには、解凍のためのコマンドが別途必要となります

例えば.gzファイルを解凍するためには、gzipコマンドが必要になるのですが、Windowsには存在せず、rgの-zオプションが動作しなかった模様。Windows版のgzipコマンドを自分でインストールすれば問題が解決します。

を以下のURLからダウンロードし、PATHを通したフォルダにコピーします。

今回は単体の「Binaries」を選び、中に含まれているgzip.exeをPATHが通ったフォルダにコピーしました。

コマンドプロンプトでgzipが実行できるのを確認した後、例えば以下のように実行します。

rg -z 札幌 utf_ken_all.csv.gz

実行後、検索結果が正常に表示されれば成功です。失敗の場合は何も表示されません。ファイル名ではなくフォルダを指定すれば、その中に含まれる.gzファイルも検索してくれます。

実はマニュアルにしっかり書かれていた

実は-zオプションの仕様はrgのmanページにしっかり書かれていました。

-z, --search-zip
This flag instructs ripgrep to search in compressed files. Currently gzip, bzip2, xz, LZ4, LZMA, Brotli and Zstd files are supported. This option expects the decompression binaries (such as gzip) to be available in your PATH. If the required binaries are not found, then ripgrep will not emit an error messages by default. Use the --debug flag to see more information.

Note that this flag does not make ripgrep search archive formats as directory trees. It only makes ripgrep detect compressed files and then decompress them before searching their contents as it would any other file.

This overrides the --pre flag.

This flag can be disabled with --no-search-zip.


このフラグは、ripgrep に圧縮ファイル内も検索させるよう指示します。現在サポートされているのは、gzip、bzip2、xz、LZ4、LZMA、Brotli、および Zstd の各形式です。このオプションを使用するには、対応する解凍用バイナリ(例:gzip)が PATH に含まれている必要があります。必要なバイナリが見つからない場合でも、ripgrep はデフォルトではエラーメッセージを出力しません。詳細情報を確認するには --debug フラグを使用してください。

なお、このフラグは、ripgrep にアーカイブ形式のファイルをディレクトリツリーとして検索させるわけではありません。ripgrep は圧縮ファイルを検出し、それを解凍してから通常のファイルと同様にその内容を検索します。

このフラグは --pre フラグを上書きします。

この機能は --no-search-zip フラグで無効にできます。

-zオプションでサポートされているのは、gzip、bzip2、xz、LZ4、LZMA、Brotli、Zstd形式で、使用するには対応する解凍用バイナリがPATHに含まれている必要があるとのこと。

必要なバイナリが見つからない場合、ripgrep はデフォルトではエラーメッセージを出力しないものの、--debugフラグで詳細を確認することができます。例えば以下のようなメッセージが表示されます。

C:\temp>rg -z --debug utf_ken_all.zip
rg: DEBUG|rg::flags::parse|crates/core\flags\parse.rs:97: no extra arguments found from configuration file
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1083: number of paths given to search: 0
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1108: using heuristics to determine whether to read from stdin or search ./ (is_readable_stdin=false, stdin_consumed=false, mode=Search(Standard))
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1118: heuristic chose to search ./
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1269: found hostname for hyperlink configuration: buff
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1279: hyperlink format: ""
rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:174: using 12 thread(s)
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: bzip2: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: bzip2: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: xz: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: xz: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: lz4: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: xz: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: brotli: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: zstd: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: zstd: could not find executable in PATH
rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: uncompress: could not find executable in PATH
rg: DEBUG|globset|crates\globset\src\lib.rs:453: built glob set; 0 literals, 0 basenames, 2 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 0 regexes
rg: DEBUG|grep_printer::standard|D:\a\ripgrep\ripgrep\crates\printer\src\standard.rs:892: ignoring utf_ken_all.zip: found binary data at offset 5

うまくいかない場合、この方法で原因を調査することができます。

まとめ

Windows版のripgrepで圧縮ファイルを検索する方法を説明しました。macOSやLinuxの場合、解凍コマンドが標準でインストールされている事が多く、何もしなくても-zオプションが使用できることが多いと思います。大してWindowsの場合は自力で解凍コマンドをインストールする必要がありそうです。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次