How to search for text within compressed file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to search for text within compressed file
# 1  
Old 07-26-2005
Question How to search for text within compressed file

I was wondering if there's a way to search within a file that's been compressed. i.e. if file a is inside file a.zip or a.gz, is there a a command that will retrieve the string of data I'm looking for in file a, and list which compressed file it found it in?

Please help!

Thanks.

Last edited by HLee1981; 07-26-2005 at 04:46 PM..
# 2  
Old 07-26-2005
man gunzip
man grep

gunzip -c file.gz | grep pattern

Last edited by vgersh99; 07-26-2005 at 03:53 PM..
# 3  
Old 07-26-2005
or...
zcat file.gz | grep foo
# 4  
Old 07-27-2005
Question

1) What if I do not know which file contains the string of text I am looking for?

2) Will this work for only .gz files, or for other compressed files as well, i.e. .zip, etc.?

Thanks!
# 5  
Old 07-27-2005
Quote:
Originally Posted by HLee1981
1) What if I do not know which file contains the string of text I am looking for?
That's what grep does best. Given some files to grep, it searches for the pattern in those files. So you need not worry about that. It will throw up all searches which are successful. i.e. files which contain the pattern.

vino
# 6  
Old 07-27-2005
Quote:
Originally Posted by fdarkangel
or...
zcat file.gz | grep foo
this can lead to Binary file (standard input) matches,...

to display strings type

zcat file.gz | strings | grep foo

best regards!
# 7  
Old 07-28-2005
If you have an upto date versoin of gzip installed you probably have zgrep as well which will do the lot for you.

zgrep foo <compressed file list>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a text and return the text from file

Hi I have a set of input strings in a pattern as given below string1 string2 string3 string4 string5 I need to search this sequence of strings from a file in such a way that the first two strings (string1 and string2) and last two strings (string4 and string5) should match with the... (8 Replies)
Discussion started by: my_Perl
8 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Search compressed files with awk and get FILENAME

I have many compressed files I want to search using awk and want to print some file contents along with the filename it came from on each output record (I simplified awk command). Here are the results with the files uncompressed: awk '{print FILENAME, $0}' test*.txt test1.txt from test1... (3 Replies)
Discussion started by: mjf
3 Replies

4. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

5. 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

6. Shell Programming and Scripting

How do I search on a text file?

Hi, I'm new to shell programming and I have a text file with data with looks like this: chocolate:toblerone swiss:23:100:20 chocolate:ferro rocher:30:90:80 gummi bear:gummi co:5:20:12 . It is basically a file which stores food. The 1st field is the type of food, 2nd... (4 Replies)
Discussion started by: mofako
4 Replies

7. UNIX for Dummies Questions & Answers

How to distribute compressed files as text?

Hello everybody, I've seen some text documents where they publish blocks of text and tell you to save it as "file.tgz" for example, and when you decompress the file, it actually works. How is that done? is there a program? Because i tried cat and doesn't work, tried less, more, hexedit and... (2 Replies)
Discussion started by: semash!
2 Replies

8. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. UNIX for Advanced & Expert Users

Search first line of compressed file

I want to read a directory full of compressed files and move the file to another directory if it meets certain criteria. I only want to look at the first line of the compressed file and if I find the string, do the move. I am currently using the following: zgrep -R -L... (1 Reply)
Discussion started by: cbreiner
1 Replies
Login or Register to Ask a Question