[ACCEPTED]-How can I grep for a text pattern in a zipped text file?-grep

Accepted answer
Score: 27

zgrep on Linux. If you're on Windows, you can 2 download GnuWin which contains a Windows port 1 of zgrep.

Score: 5

The PowerShell Community Extensions (PSCX) include Read-Archive and Expand-Archive cmdlets, but don't 5 (yet?) include a navigation provider which 4 would make what you want very simple. That 3 said, you could use Read-Archive and Expand-Archive. Something like 2 this untested bit

Read-Archive -Path foo.zip -Format Zip | `
   Where-Object { $_.Name -like "*.txt" } | `
      Expand-Archive -PassThru | select-string "myRegex"

would let you search without 1 extracting the entire archive.

Score: 3

There are some zip related commandlets in 7 the Powershell Community Extensions (PSCX). I 6 don't think they would do what you want 5 however (I could be entirely wrong about 4 that though). Instead I would use .Net Zip 3 Library (DotNetZip) which allows you to essentially 2 list the names of the files in an archive 1 then extract just the ones you want.

Score: 1

The suggested zgrep answer above only searches 6 .gz files, not zip files. If you want to 5 grep zip, gz, tar, tgz, bz, lz4, zstd, and 4 other compressed files and archives, then 3 you could use ugrep, which allows to do that 2 with -z flag. There is also a ugrep.exe version 1 for Windows in the releases and repo.

More Related questions