刚刚在以下测试文件上尝试了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 <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 414的回答):
试图升级一下,但遇到了更微妙的问题(所以看起来只有最新或特定的版本才能正常工作):
[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发布于 2015-07-16 17:14:00
我看不出你描述的错误:
$ 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.https://stackoverflow.com/questions/31460433
复制相似问题