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
Best Linux Filesystem Filter Option
How to Delete 5 Lines Before and 6 Lines After Pattern Match Using Sed
Program Life in Terms of Paged Segmentation Memory
How to Enable Mixed Mode Debugging in Visual Studio Code
Why Disable One Local Interrupt or Preemption Can Cause The Whole System with 4 Cpus Unresponsive
How to Measure Net Used Disk Space Change Due to Activity by a Given Process in Linux
Mongodb (Result= Signal, Code = Killed, Signal = Ill
Nasm X86_64 Assembly in 32-Bit Mode: Why Does This Instruction Produce Rip-Relative Addressing Code
How to Lock The Cursor to The Inside of a Window on Linux
Dreaming of Making My Own Os- What Should I Use? (Suggestions)
Where The Structure "Struct Page" Is Stored on The Linux Kernel
How to Prevent Remote Branch Deletion in Git Without Using Gitolite
Socket Programming Send() Return Value
Linux + Verify If File Is Text or Binary
Transliteration Script for Linux Shell
Svn Keeps Prompting Me for Passwords and Refuses to Cache My Credentials
How to Delete The Line That Matches a Pattern and The Line After It with Sed