Grep -F on Files in a Zipped Folder

grep -f on files in a zipped folder

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.

How to use grep command on zip files

zipgrep will work with zip files only.
If you want to grep all files, not only zipped files, then you could use ugrep, which allows to do that with -z flag.

grep a pattern in list of zip files recursively

for i in $(find . -name "*.gz"); do gzcat $i|grep -qe "n1" -e "n2" && echo $i; done

Using grep -m to save X amount of lines into new zipped file

Suggesting an awk script:

 awk 'count < 101 && /^@/ {++count; print}' input.txt

grep filenames that contain string in a zip folder

You don't need to call unzip if you're using zipgrep. Just use zipgrep directly on a zip file like this:

zipgrep -li "Token" results.zip

Note that there is no need to use -r (recursive) option here.

Search a String in folder containing zips of text files

You can use zgrep, which has the same semantics as grep, but can search within compressed files:

$ zgrep -Ril "My_Name"

Find, unzip and grep the content of multiple files in one step/command

find lets you invoke a command on the files it finds:

find /somedir/server* -type f -name log.gz -exec gunzip -c '{}' + | grep ...

From the man page:

-exec command {} +

This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of
invocations of the command will be much less than the number
of matched files. The command line is built in much the same
way that xargs builds its command lines. Only one instance of
{} is allowed within the command, and (when find is being
invoked from a shell) it should be quoted (for example, '{}')
to protect it from interpretation by shells. The command is
executed in the starting directory. If any invocation with
the + form returns a non-zero value as exit status, then
find returns a non-zero exit status. If find encounters an
error, this can sometimes cause an immediate exit, so some
pending commands may not be run at all. This variant of -exec
always returns true.



Related Topics



Leave a reply



Submit