我有一个问题我希望有人能帮助...
我正在我的一个程序中使用以下命令在压缩文件夹上执行递归fgrep/grep -f搜索:
我正在使用的命令
grep -r -i -z -I -f /path/to/pattern/file /home/folder/TestZipFolder.zip
Run Code Online (Sandbox Code Playgroud)
在模式文件中是我试图搜索的字符串"狗".
在压缩文件夹中有许多包含字符串"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
Run Code Online (Sandbox Code Playgroud)
我如何获得已经找到字符串"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
Run Code Online (Sandbox Code Playgroud)
沿着这些方向的东西,用户可以看到文件中可以找到字符串的位置(如果在不是zip文件的文件上运行grep命令,实际上可以获得此格式的输出).
非常感谢你对此的帮助,谢谢
bla*_*ith 10
如果您需要多行输出,请更好地使用zipgrep:
zipgrep -s "pattern" TestZipFolder.zip
Run Code Online (Sandbox Code Playgroud)
的-s是抑制错误消息(可选).此命令将打印每个匹配的行以及文件名.如果要删除重复的名称,当文件中有多个匹配项时,必须使用loops/grep或awk或sed完成其他一些处理.
实际上,zipgrep是egrep和unzip的组合.它的用法如下:
zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...]
Run Code Online (Sandbox Code Playgroud)
所以你可以传递任何egrep选项.