" zgrep -H"中是否有错误?

时间:2015-07-16 17:05:55

标签: linux bash shell grep

在以下测试文件中尝试了grep和zgrep:

[user@host:~]$ cat /tmp/test
2015-07-16 14:01:24 aaaa
2015-07-16 14:02:57 bbbb

我还需要打印文件名,因此我添加了-H开关并解决了以下问题:

[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。取决于命令行中-H开关的位置 - 我的行为不同,问题总是重现:(

还有一个测试:

[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

以下是我使用的zgrep概述:

[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

任何线索,如果它是一个已知的错误或我做错了什么? 任何替代品? (比如调用常规grep然后以某种方式在文件名前添加它而不需要复杂的临时文件或开销)

更新(感谢dimo414&#39;答案):

试图升级一点并遇到更微妙的问题(所以看起来只有最新或特定版本才能正常工作):

[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

1 个答案:

答案 0 :(得分:1)

我没有看到您描述的错误:

$ 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

这可能是一个已知且已修复的错误,您正在运行旧版grep和/或zgrepzgrep --version的输出会很有用,在我的机器上是:

$ 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.