48

I want to grep for today's date from zip file. How can do this?

I have a zip file called sen2616.z I want to get all the data for today's date 09.02.2014

Martini_geek
  • 629
  • 1
  • 5
  • 8
  • 2
    possible duplicate of [How can I grep for a text pattern in a zipped text file?](http://stackoverflow.com/questions/1249798/how-can-i-grep-for-a-text-pattern-in-a-zipped-text-file) – fedorqui Sep 02 '14 at 13:40

4 Answers4

48

Please use zipgrep.

zgrep is for gz files, not for zip files.

Krzysztof
  • 709
  • 6
  • 12
  • 4
    Note that one (quite annoying) limitation of `zipgrep` is that it prints only the name of the matching file ***within*** the `.zip` archive, and not the name of the .zip file itself. This is probably what you want when you're searching through a ***single*** file, but when searching recursively, this info is useless, since you don't know which .zip file matched. I had to use something like `$ for f in $(find .) ; do zipgrep -l $f && echo $i ; done` which more or less does the job. – Mike May 21 '20 at 00:44
  • @Mike, well, at least you're one step away from multithreading it ;) – hmijail Jun 16 '20 at 02:51
  • Actually, zipgrep is a shell script, so probably you could add a line just mentioning the zipfile that contained a match. – hmijail Jun 16 '20 at 02:58
  • 1
    @hmijailmournsresignees, that's true, I also noticed that zipgrep is just a shell script, however after 5 seconds of looking into it, I found it too overwhelming to find the correct position to add an `echo` so I discarded the idea. The above one liner of a shell "script" was much easier, though surely less efficient than changing the actual `zipgrep` script. Also note that changing the original script under `/usr/bin` can be problematic with updates of your distro, since it will either overwrite your change, or detect the change and ask what you want to do about it. – Mike Jun 18 '20 at 21:00
35

In my case zgrep didn't work on a normal .zip file. I had to use zipgrep instead.

Johann
  • 4,107
  • 3
  • 40
  • 39
  • 4
    Holy smoke. I wasted an hour because zgrep does not complain on a zip file. Thanks for pointing me to zipgrep. – mvw Jul 02 '19 at 12:10
  • 3
    Additional info: On Debian & co. `zipgrep` is provided by the `unzip` package. – jlh Sep 09 '19 at 17:37
  • @mvw, zgrep does not "complain" on non-gzip files, because it can handle both gzip and plain-text files. Anything that is not a valid gzip compressed data, is treated as plain text and is searched as-is. This is true even if it's a binary file, like .zip, because normal grep can also search through binary data. So this is not a bug, it's a feature. – Mike May 21 '20 at 00:16
  • 1
    @Mike I am tending more towards a usability bug. A warning message would have helped, as my intention was to search compressed files, otherwise I would have used grep. Since then I am more careful and always try to check for a sure hit first before I do serious work. – mvw May 21 '20 at 17:07
  • @mvw, this is getting more of a side discussion, but anyway - "usability" (for humans) is not a top priority in command-line tools :-) Scriptability is. A script calling grep assumes that every line in the output is a match, and if there are no matches, then nothing is printed. A line saying "oh, we found nothing, but that's maybe because it's the wrong file type" is of great help to a human, but is a deal breaker for automated handling of the output. – Mike May 22 '20 at 18:25
  • Puh.. I delivered a wrong bug analysis because of this behaviour. I had to search lots of zip files and I DID check for a sure hit and zgrep found one, but it failed to find the ones I was actually interested in that where indeed present. The zip files apparently used different compression mechanisms. Some of them zgrep could decompress, others not, but as stated in other comments - it didn't complain about that. I doubt that this should even be the case for CLI tools. It's often the case, that you can't control your incoming data. I prefer failing fast and taking care of output expectations.. – Klaus Jul 12 '21 at 08:27
12

You can use zgrep to get what you want, with:

zgrep '09.02.2014' myfile.zip

See man zgrep for more info.

Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • 1
    zgrep didn't work on windows+cygwin on epub files, zipgrep worked. – mosh Aug 29 '17 at 16:26
  • 3
    `zgrep` silently failed for me on a zip file: it gave no error/warning message, and returned only a _subset_ of the matches! Had to use `zipgrep`. – gd1 Jan 18 '18 at 10:54
  • While zgrep is great, this is not correct because zgrep does not search zip files but searches gz files. They aren't the same. – Dr. Alex RE Feb 05 '20 at 13:51
  • 1
    @gd1, @mosh - guys, pls understand that .gz and .zip are two different archive formats. They have nothing in common, even if they sound similar. Therefore, zgrep and zipgrep are different tools for different purposes - they are not competing products that do the same job, and the one "works" and the other "fails". If you apply the wrong tool for the job, you'll never get the right result. `zgrep` does not "silently fail" - it just does not print anything if it doesn't find anything. And it doesn't find anything, because it treats your .zip archive as a normal binary file (since it's not gzip). – Mike May 21 '20 at 00:28
7

zipgrep will work with zip files only. If you want to grep all files, not only zipped files, then you could use ugrep, which allows to do that with -z flag.

Shmidt
  • 16,436
  • 18
  • 88
  • 136