8

我有一个问题,我希望有人能够帮助解决...

我在我的一个程序中使用以下命令对压缩文件夹执行递归 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 命令,您实际上会以这种格式获得输出)。

非常感谢您的帮助,谢谢

4

1 回答 1

11

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

zipgrep -s "pattern" TestZipFolder.zip

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

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

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

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

于 2013-08-02T12:01:07.357 回答