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

时间:2013-08-02 11:31:44

标签: 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命令,则实际上以此格式获取输出)。

非常感谢您对此的帮助,谢谢

1 个答案:

答案 0 :(得分:10)

如果您需要多行输出,请更好地使用 zipgrep

zipgrep -s "pattern" TestZipFolder.zip
-s 用于禁止错误消息(可选)。此命令将打印每个匹配的行以及文件名。如果要删除重复的名称,当文件中有多个匹配项时,必须使用loops / grep或awk或sed完成其他一些处理。

实际上, zipgrep egrep 解压缩的组合。它的用法如下:

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

所以你可以传递任何egrep选项。