tags:

views:

52

answers:

2

for ex abc.tar.gz has

abc/file1.txt
abc/file2.txt
abc/abc1/file3.txt
abc/abc2/file4.txt

i need to read/display the contents of file3.txt without extracting the file.

Thanks for any input.

+3  A: 
import tarfile
spam = tarfile.open( "abc.tar.gz" )
if "abc/abc1/file3.txt" in spam.getnames():
    with spam.extractfile( "abc/abc1/file3.txt" ) as ham:
        print ham.read()

See tarfile.

katrielalex
thanks for ans, but my vote goes to bash since its easy for me to pipe to look for patterns on the particular file.
szhak
Fair enough -- though you could do the same thing in python; `getnames` returns an array of files in the archive that you could search/analyse. Is there any particular reason the question is tagged `python`, btw? =p
katrielalex
+2  A: 
tar -xzf mytar.tar.gz --to-command=cat filename.in.archive
adamk
your ans helped. i was looking for some pattern in the particular file. pipe to csplit helped me tar -xzf file.tar.gz -O file/file.txt |csplit -s - "/regex /" {no_of_times} > /dev/null thanks
szhak