刚刚在以下测试文件上尝试了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
例如,取决于命令行中
-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 <[email protected]>
# 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的回答):
尝试升级一点,并面临更微妙的问题(因此看起来只有最新版本或特定版本工作正常):
[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
最佳答案
我看不出你所描述的错误:
$ 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
和/或zgrep
。zgrep --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.