简体   繁体   English

如何使用 zgrep 在与给定文件名模式匹配的存档文件中查找内容?

[英]How do I use zgrep to look for content in archived files matching a given filename pattern?

Suppose I have two tar.gz files假设我有两个 tar.gz 文件

a1.tar.gz
a2.tar.gz

and each archive contains many files, including a file called并且每个存档都包含许多文件,其中包括一个名为

target.txt

How do I search for BLAH in target.txt in both of these archives using zgrep without searching all of the other files in each archive?如何寻找BLAHtarget.txt均使用zgrep这些档案没有搜索所有其他文件中每个存档?

If I try如果我尝试

zgrep -a BLAH *.tar.gz

then that searches all files in each archive, and if I try然后搜索每个存档中的所有文件,如果我尝试

zgrep --include=target.txt -a BLAH *.tar.gz

then I get然后我得到

zgrep: --include=target.txt: option not supported

You can use zgrep , however this command will not locate the file you are looking for as it searches the entire tar-formatted file for matches.您可以使用zgrep ,但是此命令不会定位您要查找的文件,因为它会在整个 tar 格式的文件中搜索匹配项。 This will report which tar file has a match for BLAH but this does not limit searching tar files containing the file target.txt .这将报告该tar文件有一个匹配BLAH但这并不限制搜索包含文件的tar文件target.txt

There is an open source tool called ugrep to search archives (zip, tar, pax, cpio, jar) and tar.gz tarballs.有一个名为ugrep 的开源工具用于搜索档案(zip、tar、pax、cpio、jar)和 tar.gz tarball。 Use option -z and -g target.txt to search for BLAH in target.txt in all of the tar files found recursively:使用选项-z-g target.txt搜索BLAHtarget.txt在所有焦油找到的文件递归的:

ugrep -z "BLAH" -g target.txt

To search the working directory only without recursing deeper:只搜索工作目录而不深入递归:

ugrep -z "BLAH" -g target.txt .

Note that option -g takes a glob.请注意,选项-g需要一个 glob。 Use a quoted glob like -g "*.txt" to match all .txt files.使用像-g "*.txt"这样的带引号的 glob 来匹配所有 .txt 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在终端中使用grep打印与特定grep模式匹配的文件列表? - How do I use grep in the terminal to print a list of files matching a specific grep pattern? 用于zgrep的grep4j,即grep归档的日志文件 - Grep4j for zgrep ie., grepping archived log files 如何查找不包含给定字符串模式的文件? - How do I find files that do not contain a given string pattern? ZGrep 模式*after* 给定行的第一次出现 - ZGrep for first occurence of pattern *after* given line 如何检索 linux 中给定目录级别的所有文件。 我想要绝对路径以及文件名 - How do I retrieve all files in linux for given directory level. I want to absolute path aswell as the filename 如何从一组文件中删除与模式匹配的所有行? - How do I remove all lines matching a pattern from a set of files? zgrep到日志中的搜索模式 - zgrep to search pattern in log 如何在与命令行中的另一个模式匹配的目录中“查找”匹配模式的文件? - How can I `find` files matching pattern within a directory matching another pattern from the command line? 如何 grep 从模式文件到文本文件的匹配文件名和扩展名? - How to grep a matching filename AND extension from pattern file to a text file? 如何在unix中为给定字符串创建kmer模式? - How do I create pattern of kmer in unix for a given string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM