Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep from tar.gz without extracting [faster one]

Tags:

linux

grep

bash

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
like image 735
Pixel Avatar asked Dec 21 '12 02:12

Pixel


People also ask

Can we grep in tar file?

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!

Can I grep a gz file?

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.

What is the difference between grep and zgrep?

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.


2 Answers

If you have zgrep you can use

zgrep -a string file.tar.gz
like image 102
lanes Avatar answered Oct 21 '22 03:10

lanes


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
like image 44
Jester Avatar answered Oct 21 '22 04:10

Jester