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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to grep/read a file inside compressed tgz without extract?
# 1  
Old 03-08-2010
Bug 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:
Code:
a.txt
b.txt
c.txt

Code:
abc.tgz

Code:
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!

Last edited by radoulov; 03-08-2010 at 06:43 AM.. Reason: Please use code tags!
# 2  
Old 03-08-2010
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
Code:
zip test.tgz a.txt b.txt c.txt

Then use the following command to search the pattern in the .tgz file
Code:
 
 zipgrep  "d" test.tgz

The output is
a.txt:d
b.txt:d
# 3  
Old 03-08-2010
If you have/can install the Perl modules Archive::Tar and IO::Zlib:

Code:
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


Last edited by radoulov; 03-08-2010 at 07:29 AM..
# 4  
Old 03-08-2010
Thanks all for your suggestion. I will look into them to see which one fit my need. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Case inside While read File

Hi Experts, Need your guidance for case statement. I tried many way but no success yet.Now my existing code is doing something like below. Each Line of the input file contains one test case.#!/bin/bash FILE=$1 while read LINE; do do COMMAND done < $FILE Now I want to modify the code... (6 Replies)
Discussion started by: pradyumnajpn10
6 Replies

2. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

3. Shell Programming and Scripting

Extract compressed tarball to folder?

This is for Red Hat Enterprise Linux Client release 5.7 (Tikanga). Wanna extract entire contents of a tar.gz to a folder of my choosing. Thanks (2 Replies)
Discussion started by: stevensw
2 Replies

4. UNIX for Advanced & Expert Users

how to read a file inside jar

how to read the text file inside the jar without extracting jar. (3 Replies)
Discussion started by: karthikn
3 Replies

5. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

6. UNIX for Dummies Questions & Answers

Reading compressed files during a grep search

All, The bottom line is that im reading a file, storing it as variables, recursively grep searching it, and then piping it to allow word counts as well. I am unsure on how to open any .zip .tar and .gzip, search for keywords and return results. Any help would be much appreciated! Thanks (6 Replies)
Discussion started by: ryan.lee
6 Replies

7. UNIX for Dummies Questions & Answers

file.tgz.1of2 & file.tgz.2of2

Hi all, Need help. Anybody seen this kind of file before? file.tgz.1of2 file.tgz.2of2 how to extract this tgz file? Any help? Tq (5 Replies)
Discussion started by: zeedwolf
5 Replies

8. Shell Programming and Scripting

doing grep inside perl file

Hi have one Perl file inside that i am defining at an array file. @temp_vmdk_files = `grep vmdk '$guest_vmx'` where my $guest_vmx=/vmfs/volumes/47e40fec-9c8bb7f7-d076-001422159f8a/BES Exchange/BES-Exchange.vmx and i am just want to do grep of "vmdk" files from the above path but when... (5 Replies)
Discussion started by: bp_vardhaman
5 Replies

9. UNIX for Dummies Questions & Answers

Extract .tgz files that only contain a pattern

Let's say I've got File.tgz that contains:- Apple.txt Banana.txt Carrot.txt AppleBanana.txt Now, I would like to only extract files that contain the patter "Apple". I've tried this tar -xvf File.tgz 'tar -tf File.tgz | grep 'Apple'' but it does not work. Please help. Thanks. (12 Replies)
Discussion started by: percivalwcy
12 Replies

10. Shell Programming and Scripting

grep inside the zip file

i have to grep a particular pattern say "meter number" in 100s of zip files file1.zip : : : : file100.zip how to achive this? cat *.zip | grep "meter number" also i dnt want to unzip and then grep it...... hope i made myself clear. thanks in advance (5 Replies)
Discussion started by: ali560045
5 Replies
Login or Register to Ask a Question