bash zgrep - 列出匹配的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8220905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
zgrep - list matched file name
提问by tamasgal
grep or egrep show the filename of the matched file when you define the option "-l".
当您定义选项“-l”时,grep 或 egrep 显示匹配文件的文件名。
However, I've got a huge file (tar.gz) which contains thousands of files and I need to find out which of them contains a pattern. The problem is:
但是,我有一个包含数千个文件的巨大文件 (tar.gz),我需要找出其中哪些包含模式。问题是:
zgrep -la testPattern HugeFile.tar.gz
gives me only the name of the tar.gz file, not the file in it, which matches the pattern.
只给我 tar.gz 文件的名称,而不是其中与模式匹配的文件。
This seems to be a bug for me, but maybe I misunderstood something… Does anyone know how to do it without gunzipping or untaring the archive?
这对我来说似乎是一个错误,但也许我误解了一些东西......有没有人知道如何在不压缩或解压存档的情况下做到这一点?
回答by Drav Sloan
zgrep'ing a tar.gz will not do what you expect it to do (it is effectively grepping the raw tar image).
zgrep'ing tar.gz 不会做你期望它做的事情(它有效地 grepping 原始 tar 图像)。
You can however take the alternative approach as answered here in other questions:
但是,您可以采用在其他问题中回答的替代方法:
回答by Ken Cheung
zgrep is a shell script which use gzip to decompress the files to stdout and pipe into grep. It cannot tell which file inside the tarball hit the match.
zgrep 是一个 shell 脚本,它使用 gzip 将文件解压缩到标准输出并通过管道传输到 grep。它无法判断 tarball 中的哪个文件命中了匹配项。
One possible way you may try: install the archivemount which use fuse and libarchive, allows you to "mount" a tarball to as if a file system. Then you should be able to grep under the mount point. I didn't try it before, but its better than untar everything for a grep.
您可以尝试一种可能的方法:安装使用 fuse 和 libarchive 的存档挂载,允许您像文件系统一样“挂载”tarball。然后您应该能够在挂载点下进行 grep。我之前没有尝试过,但它比为 grep 解压所有内容要好。

