Grep -f for files in a zip folder

I have a problem, I hope someone can help with ...

I do a recursive search for fgrep / grep -f in a ZIP folder using the following command in one of my programs:

Team i use

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

Inside the template file is the line "Dog", which I am trying to execute.

There are several text files in the zip folder containing the string "Dog".

The grep -f command successfully finds text files containing the string “Dog” in 3 files in a folder with a zipper, but prints the output on one line, and at the end some strange characters appear, that is PK (as shown below). And when I try to print the output to a file in my program, other characters appear at the end, such as ^B^T^@

The output of the grep -f command:

 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 

How can I get each of the files where the line "Dog" was found to print on a new line so that they are not grouped together on one line, as it is now? Also, where are the “PCs” and other strange characters that appear on the output, and how can I prevent them from appearing?

Desired Conclusion

 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 

Something in these lines, so the user can see where the line can be found in the file (you really get the output in this format if you run the grep command in a file that is not a zip file).

Your help is greatly appreciated, thanks

+8
linux grep zip
source share
1 answer

If you need multi-line output, you'd better use zipgrep:

 zipgrep -s "pattern" TestZipFolder.zip 

-s - suppress error messages (optional). This command will print all matched lines along with the file name. If you want to remove duplicate names if the file contains more than one match, some other processing should be done using / grep or awk or sed loops.

Actually, zipgrep is a combination of egrep and unzip. And its use is as follows:

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

so you can pass any egrep options to it.

+10
source share

All Articles