Just tried grep and zgrep on following test file:
[user@host:~]$ cat /tmp/test 2015-07-16 14:01:24 aaaa 2015-07-16 14:02:57 bbbb
I also need the file-name printed, so I’ve added the -H
switch and stepped on the following problem:
[user@host:~]$ grep aaaa -A1 -H /tmp/test /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test-2015-07-16 14:02:57 bbbb [user@host:~]$ zgrep aaaa -A1 -H /tmp/test /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test:02:57 bbbb [user@host:~]$ zgrep aaaa -A1 /tmp/test -H /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test:2015-07-16 14:02:57 bbbb
E.g. depending on POSITION of -H
switch in the command-line – the behavior is different for me and the issue always reproduces 🙁
One more test:
[user@host:~]$ gzip /tmp/test [user@host:~]$ zgrep aaaa -A1 -H /tmp/test.gz /tmp/test.gz:2015-07-16 14:01:24 aaaa /tmp/test.gz:02:57 bbbb [user@host:~]$ zgrep aaaa -A1 /tmp/test.gz -H /tmp/test.gz:2015-07-16 14:01:24 aaaa /tmp/test.gz:2015-07-16 14:02:57 bbbb
Here’s the outline of which zgrep I’m using:
[user@host:~]$ ls -l `which zgrep` -rwxr-xr-x 3 root root 3121 Jan 14 2010 /usr/bin/zgrep* [user@host:~]$ head `which zgrep` #!/bin/sh # zgrep -- a wrapper around a grep program that decompresses files as needed # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca> # Copyright (C) 1998, 2001, 2002 Free Software Foundation # Copyright (C) 1993 Jean-loup Gailly # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by
Any clue of if it’s a known bug or am I doing something wrong? Any alternatives? (like to invoke regular grep and then somehow prepend it with file-name without complex temporary files or overhead)
Update (thanks to dimo414’s answer):
Tried to upgrade a bit and faced even more subtle problems (so it looks like only latest or specific versions are working properly):
[user@rhel66:~]$ grep --version|head -1 GNU grep 2.6.3 [user@rhel66:~]$ zgrep --version|head -1 zgrep (gzip) 1.3.12 [user@rhel66:~]$ grep aaaa -A1 -H /tmp/test /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test-2015-07-16 14:02:57 bbbb [user@rhel66:~]$ zgrep aaaa -A1 -H /tmp/test gzip: -A1.gz: No such file or directory gzip: -H.gz: No such file or directory /tmp/test:2015-07-16 14:01:24 aaaa [user@rhel66:~]$ zgrep aaaa -A1 /tmp/test -H gzip: -A1.gz: No such file or directory /tmp/test:2015-07-16 14:01:24 aaaa gzip: -H.gz: No such file or directory
Advertisement
Answer
I do not see the error you describe:
$ grep aaaa -A1 -H /tmp/test /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test-2015-07-16 14:02:57 bbbb $ zgrep aaaa -A1 -H /tmp/test /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test:2015-07-16 14:02:57 bbbb $ zgrep aaaa -A1 /tmp/test -H /tmp/test:2015-07-16 14:01:24 aaaa /tmp/test:2015-07-16 14:02:57 bbbb
This is likely a known and fixed bug, and you’re running an old version of grep
and/or zgrep
. The output of zgrep --version
would be helpful, on my machine it’s:
$ grep --version grep (GNU grep) 2.16 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>. $ zgrep --version zgrep (gzip) 1.6 Copyright (C) 2010-2013 Free Software Foundation, Inc. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law. Written by Jean-loup Gailly.