Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep -f on files in a zipped folder

Tags:

linux

grep

zip

I am performing a recursive fgrep/grep -f search on a zipped up folder using the following command in one of my programs:

The command I am using:

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

Inside the pattern file is the string "Dog" that I am trying to search for.

In the zipped up folder there are a number of text files containing the string "Dog".

The grep -f command successfully finds the text files containing the string "Dog" in 3 files inside the zipped up folder, but it prints the output all on one line and some strange characters appear at the end i.e PK (as shown below). And when I try and print the output to a file in my program other characters appear on the end such as ^B^T^@

Output from 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 would I get each of the files where the string "Dog" has been found to print on a new line so they are not all grouped together on one line like they are now? Also where are the "PK" and other strange characters appearing from in the output and how do i prevent them from appearing?

Desired output

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 along these lines, whereby the user is able to see where the string can be found in the file (you actually get the output in this format if you run the grep command on a file that is not a zip file).

like image 983
yonetpkbji Avatar asked Aug 02 '13 11:08

yonetpkbji


People also ask

Does grep work on zipped files?

Unfortunately, grep doesn't work on compressed files. To overcome this, people usually advise to first uncompress the file(s), and then grep your text, after that finally re-compress your file(s)… You don't need to uncompress them in the first place. You can use zgrep on compressed or gzipped files.

How do I search for files in a ZIP file?

You may open the Search window, from the Manage tab of an open WinZip file. Use the Search option to search for a particular file or set of files within the currently viewed folder or the entire Zip file and select them.

How do I search for a ZIP file in Unix?

If your company uses one of the Unix versions, there are multiple methods for viewing the contents of a ZIP file without decompressing the file. The contents are printed to the screen but the file remains intact. Three commands that are included with many Unix versions are "uncompress," "zcat" and "unzip."


1 Answers

If you need a multiline output, better use zipgrep :

zipgrep -s "pattern" TestZipFolder.zip

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

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

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

so you can pass any egrep options to it.

like image 134
blackSmith Avatar answered Sep 22 '22 02:09

blackSmith