How to grep the contents inside a tar file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep the contents inside a tar file
# 1  
Old 01-05-2011
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.

HTML Code:
> cat a
123
456
HTML Code:
>cat b
789
000
I create a tar file one.tar containing files a and b

HTML Code:
tar -cvf one.tar a b
a a 1K
a b 1K
I used tar -tf and tar -tvf options but no luck

HTML Code:
> tar -tvf one.tar | grep 123
tar: blocksize = 6
Can someone pls help here ? Am on Sun OS m/c

Thanks
W
# 2  
Old 01-05-2011
The command tar -tvf one.tar will only list the contents of the archive, not the contents of the contents? Smilie 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
# 3  
Old 01-05-2011
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.


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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem to grep contents from a file

hi, I'm trying to grep content from one file in another file. The file that I'm searching into is large and hence I need to temporarily unzip it first. gzip -dc ALL.chr2.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz | grep '169997024\|190670539\|128051369' >... (2 Replies)
Discussion started by: janshamsani
2 Replies

2. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

3. Shell Programming and Scripting

unzip a file then further check contents inside it

Dear all I need to unzip a file then further it has many folders to perform tak inside each folder. What Task I need to perform I'm able to do it but once I unzip a folder then I'm not able to do cd folder/ into it. I have written following code for it for i in $( ls | grep Run_20111016 ) do... (2 Replies)
Discussion started by: Bhalinder
2 Replies

4. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

5. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

6. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

7. Solaris

Listing Contents of tar.gz file

Hi fellows, Can you please share any command with which I can list down the file names inside a tar.gz file. I have tried with these possibilities but in vain. bash-3.00$ tar -ztvf file.tar.gz tar: z: unknown function modifier bash-3.00$ tar ztvf file.tar.gz tar: z: unknown function... (1 Reply)
Discussion started by: Zaib
1 Replies

8. Shell Programming and Scripting

find file and print only contents with a hit by grep

Hi, can someone help me. I have some files and search a content in this files. If i have a hit I will print a output: filename:content But are more hits in one file: The output is always filename:content E.G. Seach about "three" file1 {one, two, three, four, three} file2... (5 Replies)
Discussion started by: Timmää
5 Replies

9. AIX

how to take tar backup of the contents of a file

Hi I have a file named files.2.backup which holds the location of some directory and file i,e $ cat files.2.backup /d01/app/oracle/product/7.3.2/dbs/fortest_syst_01.dbf /d01/app/oracle/product/7.3.2/dbs/fortest_temp_01.dbf /d01/app/oracle/product/7.3.2/dbs/fortestdata_01.dbf... (5 Replies)
Discussion started by: sumanbangladesh
5 Replies

10. UNIX for Dummies Questions & Answers

how to strip out the contents of file using grep

Hi, I am receving a file from remote site which has EDI information for 830, 862 and 997 and I want to extect the data for 997 using grep's or any other methods. The data look like this: ISA~000 0000-0001-1000~997 AK1000~m 000~IEA~M ISA~000 0000-0001-1000~849 000~IEA~M ISA~000... (11 Replies)
Discussion started by: isingh786
11 Replies
Login or Register to Ask a Question