I have multiple *.tar.gz files. They all contain files with file name a, b, and c. Is there a way to grep string 'foo' only from file b under these multiple *.tar.gz files without extracting the files.
Asked
Active
Viewed 2,460 times
2 Answers
6
Assuming that by "without extracting" you mean "without saving extracted files to disk":
for file in *.tar.gz ; do
tar xzOf $file b | grep --label=$file/b -H foo
done
tar options:
x
extract
z
gunzip before extracting
O
dump to stdout rather than creating file (capital letter oh, not number zero)
f
from specified tar file
grep options (suggested by JJoao)
--label=...
use specified label as the filename
-H
print filename for each match

user4556274
- 8,995
- 2
- 33
- 37