Am trying to grep pattern from dozen files .tar.gz but its very slow
am using
tar -ztf file.tar.gz | while read FILENAME
do
if tar -zxf file.tar.gz "$FILENAME" -O | grep "string" > /dev/null
then
echo "$FILENAME contains string"
fi
done
On the other side, the grep command has the –label=LABEL option to display LABEL as the filename. It's pretty useful when we grep on Stdin. Therefore, to solve the problem, we can assemble a command like tar … –to-command='grep …' and pass tar's TAR_FILENAME variable to grep's –label option. It works!
gz files in your system. 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.
zgrep states it works the same as grep, but can handle zipped files. zgrep "hello" * however returns 1 even though the pattern was found in test1. it returns 1 because the pattern was not found in test2. turns out if any file does not match the pattern, 1 is returned, even if all other files do match the pattern.
If you have zgrep
you can use
zgrep -a string file.tar.gz
You can use the --to-command
option to pipe files to an arbitrary script. Using this you can process the archive in a single pass (and without a temporary file). See also this question, and the manual.
Armed with the above information, you could try something like:
$ tar xf file.tar.gz --to-command "awk '/bar/ { print ENVIRON[\"TAR_FILENAME\"]; exit }'"
bfe2/.bferc
bfe2/CHANGELOG
bfe2/README.bferc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With