How to grep the contents inside a tar file

Hi All

I have searched the possibility of this options everywhere but am unable to find it in any forum.

I have a tar file inside which there are n number of files and i dont know them. I need to grep a word inside the tar file and need to know in which file the word resides.

> cat a
123
456
>cat b
789
000

I create a tar file one.tar containing files a and b

tar -cvf one.tar a b
a a 1K
a b 1K

I used tar -tf and tar -tvf options but no luck

> tar -tvf one.tar | grep 123
tar: blocksize = 6

Can someone pls help here ? Am on Sun OS m/c

Thanks
W

The command tar -tvf one.tar will only list the contents of the archive, not the contents of the contents? :o Sorry. Not the best phrasing there.

It's like a library having a list of the books on the shelves that you can scan through, but it won't be able to find the phrases of text actually in the books themselves.

If you need to have critical things accessible in this way, you may need to create yourself some sort of a reference index for the critical entries. Otherwise, you would need to extract the files back to disk and then run the grep on the files before tidying them away, but there usually isn't space to do that and there is a lot of I/O to do too.

I supppose it might be possible to stream through the tar file looking for the file markers and the string you are searchign for, but that would surely be C code written and you would have to understand the tar file structure - which I don't.

Robin
Liverpool/Blackburn
UK

Script to tackle the problem. This cannot be done in one stage, we need to run "tar" twice. Assumes that one.tar has been prepared first.

Note this script contains code to allow me to test it in the same directory as the files "a" and "b" without clashing (I move the files out of the way temporarily).

Note the use of "tar -tf" to get a list of files with no other detail (we don't want the -v parameter). We then process the list of files and extract each file in turn and then run grep on each file.
In the real world we would clean up after temporarily extracting files - I have not done this for testing purposes only.

search="123"
tar -tf one.tar | while read filename
do
        if [ -f "${filename}" ]
        then
                mv "${filename}" "${filename}.sav"
        fi
        #
        tar -xf one.tar "${filename}"
        #
        found=`grep -l "${search}" "${filename}"`
        if [ ! "${found}""X" = "X" ]
        then
                echo "Found \"${search}\" in \"${found}\""
                grep "${search}" "${filename}"
                echo ""
        fi
        #
        if [ -f "${filename}.sav" ]
        then
                mv "${filename}.sav" "${filename}"
        fi
done

./scriptname
Found "123" in "a"
123