我在我的一个程序中使用以下命令对压缩文件夹执行递归 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 进行一些其他处理。
实际上,zipgrep 是 egrep 和 unzip 的组合。其用法如下:
zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...]
因此您可以将任何 egrep 选项传递给它。
关于linux - grep -f 压缩文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18015866/