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.
How to grep on the content of a zipped non-standard textfile
In this post on grepping non-standard text files, I found the answer:
unzip -c zipfile.zip error.log | grep -a "A.c.c.e.s.s"
Now I have something to start from.
Thanks, everyone, for your cooperation.
grep on zipped files without zgrep
This seems to be a bug in zgrep
. Try xzgrep
.
$ xzgrep -q hello *; echo $?
0
$ zgrep -q hello *;echo $?
1
$ grep -q hello *;echo $?
0
You can also use zcat
and grep
together, if files are always gzipped
.
$ zcat * | grep -q hello; echo $?
How put every files result by a grep into a zip file
You can use -l
option in grep
to output only filename and pipe it to xargs
to create zip
for each found fine:
grep --null -rlE --include='*.txt' 'string1|string2' |
xargs -0 -I {} zip '{}'.zip '{}'
Note use of --null
option with -0
in xargs
to address filenames with whitespaces, special characters.
To create a single zip file containing each matched file use:
grep --null -rlE --include='*.txt' 'string1|string2' |
xargs -0 zip test.zip
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 can I grep for a text pattern in a zipped text file?
zgrep on Linux. If you're on Windows, you can download GnuWin which contains a Windows port of zgrep.
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 ls with grep to zip selection of files
Don't parse ls
zip archive.zip 2020*
is all you need.
While bash is parsing this line for execution, it will expand the file pattern before it launches zip
.
An important thing to note about the difference between regular expressions:
- the regular expression
2020*
matches, anywhere in the string, "202" followed by zero or more "0"- the string "202" is matched by the regular expression
2020*
- the string "120201" is matched by the regular expression
2020*
- the string "202" is matched by the regular expression
- the glob pattern
2020*
matches, starting at the beginning, "2020" followed by zero or more of any character- the file "202" is not matched by the glob pattern
2020*
- the file "120201" is not matched by the glob pattern
2020*
- the file "202" is not matched by the glob pattern
Related Topics
Receiving Multicast on a Server with Multiple Interfaces (Linux)
How to Find a Tutorial on The Writing of Makefiles
What's The Difference Between Insmod and Modprobe
Why I Am Not Getting Signal Sigkill on Kill -9 Command in Bash
Why Processes Are Deprived of CPU for Too Long While Busy Looping in Linux Kernel
Ubuntu Based Docker-Machine Image
Why Use G++ Instead of Gcc to Compile *.Cc Files
Loop Through Array of Arrays of String with Spaces
"Invalid Arithmetic Operator" in Shell
What Is The Size of Coap Packet
Linux - Check If There Is an Empty Line at The End of a File
How to Redirect Http to Https Using Gcp Load Balancer
Is There Any Difference Between Executable Binary Files Between Distributions
How to Programmatically Pick and Choose Which Core of a Multi-Core CPU My Thread Should Run On