tags:

views:

269

answers:

3

I have list of files which contain particular patterns. But , those file have been tarred. Now I want to search the pattern in the tar file . And to know which are file contain pattern. without extracting it.

Any idea....?

A: 

Python's tarfile module along with Tarfile.extractfile() will allow you to inspect the tarball's contents without extracting it to disk.

Ignacio Vazquez-Abrams
A: 

The easiest way is probably to use avfs. I've used this before for such tasks.

Basically, the syntax is:

avfsd ~/.avfs # Sets up a avfs virtual filesystem
rgrep pattern ~/.avfs/path/to/file.tar#/

/path/to/file.tar is the path to the actual tar file.

Pre-pending ~/.avfs/ (the mount point) and appending # lets avfs expose the tar file as a directory.

Matthew Flaschen
+1  A: 

the tar command has a -O switch to extract your files to standard output. So you can pipe those output to grep/awk

tar xvf  test.tar -O | awk '/pattern/{print}'

tar xvf  test.tar -O | grep "pattern"

eg to return file name one pattern found

tar tf myarchive.tar | while read -r FILE
do
    if tar xf test.tar $FILE  -O | grep "pattern" ;then
        echo "found pattern in : $FILE"
    fi
done
ghostdog74
Unfortunately that won't give the names of matching files.
jkff
a little bit of scripting will do. see my edit
ghostdog74