linux - grep -f 压缩文件夹中的文件

标签 linux zip grep

我在我的一个程序中使用以下命令对压缩文件夹执行递归 fgrep/grep -f 搜索:

我正在使用的命令:

grep -r -i -z -I -f /path/to/pattern/file /home/folder/TestZipFolder.zip

模式文件中是我要搜索的字符串“Dog”。

在压缩文件夹中有许多包含字符串“Dog”的文本文件。

grep -f 命令成功地在压缩文件夹内的 3 个文件中找到包含字符串 "Dog"的文本文件,但是它将输出全部打印在一行上,并且在末尾出现一些奇怪的字符,即 PK(如图所示以下)。当我尝试将输出打印到程序中的文件时,其他字符出现在末尾,例如 ^B^T^@

grep -f 命令的输出:

TestZipFolder/test.txtThis is a file containing the string DogPKtest1.txtDog, is found again in this file.PKTestZipFolder/another.txtDog is written in this file.PK 

我如何让每个文件中的字符串“Dog”都打印在一个新行上,这样它们就不会像现在这样全部分组在一行上了? 另外,输出中出现的“PK”和其他奇怪字符在哪里?如何防止它们出现?

期望的输出

TestZipFolder/test.txt:This is a file containing the string Dog
TestZipFolder/test1.txt:Dog, is found again in this file
TestZipFolder/another.txt:Dog is written in this file

沿着这些思路,用户可以看到字符串在文件中的位置(如果您在非 zip 文件的文件上运行 grep 命令,您实际上会得到这种格式的输出)。

最佳答案

如果你需要多行输出,最好使用 zipgrep :

zipgrep -s "pattern" TestZipFolder.zip

-s 用于抑制错误消息(可选)。此命令将打印每个匹配的行以及文件名。如果要删除重复名称,当文件中有多个匹配项时,必须使用循环/grep 或 awk 或 sed 进行一些其他处理。

实际上,zipgrepegrepunzip 的组合。其用法如下:

zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...]

因此您可以将任何 egrep 选项传递给它。

关于linux - grep -f 压缩文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18015866/

相关文章:

linux - 测量应用程序的 CPU%,我应该推断吗?

html - CSS 2.1 兼容的 HTML 到 Microsoft Word 的转换?

php - ZIP 文件下载 php readfile() 错误

java - 通过 HttpResponse Java 下载 Zip 文件

sql - Oracle:跨多个列的 "grep"?

linux - linux select() 如何工作?

c - 检查备用信号堆栈(不同的分配方式)

asp.net - 如何关闭下载的 zip 文件的 HTTP 压缩?

regex - Awk,仅打印与正则表达式匹配的模式

linux - shell:从具有已知键的字符串中提取键值