how to grep/read a file inside compressed tgz without extract?

Hi all,

I would like to ask whether in Unix shell/perl have any functions or command to allow grep/cat/read a file inside compressed .tgz without extract it?

I know we can tar tvf a compressed tgz but this only allow we read the path/filename contained inside the tarball. If we want to read the file, does it possible?

Let said I have a compressed file: test.tgz
It contains:

a.txt
b.txt
c.txt
abc.tgz
a.txt:
a
b
c
d

b.txt
1
2
3
d

c.txt
f
g
h
i

Can we grep files which contains "d" inside the compressed test.tgz without extract test.tgz? (some function like gzcat can read compressed file)

Thanks for any input!

You can use the "zipgrep" command.

a.txt content
a
b
c
d

b.txt content
1
2
3
d

c.txt content
f
g
h
i

Then I zip the all files using the following command

zip test.tgz a.txt b.txt c.txt

Then use the following command to search the pattern in the .tgz file

 
 zipgrep  "d" test.tgz

The output is
a.txt:d
b.txt:d

If you have/can install the Perl modules Archive::Tar and IO::Zlib:

perl -MArchive::Tar -le'
  ($pattern, $fname) = @ARGV;
  $tar = Archive::Tar->new;
  $tar->read($fname, 1);
  $tar->get_content($_) =~ /$pattern/ 
    and print for $tar->list_files;
  ' d test.tgz

Thanks all for your suggestion. I will look into them to see which one fit my need. Thanks again.