Searching inside a .gz file without gunzip


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching inside a .gz file without gunzip
# 1  
Old 08-09-2011
Searching inside a .gz file without gunzip

I have a set of folders inside which there may be n number of files. All those files are in .gz extension. Now I need to search all the files without gunzip them. Also I need to read the content of a file if the search pattern is found without gunzip them.
# 2  
Old 08-09-2011
It's impossible to read gzipped files without gunzipping them. If you don't want change gzipped files use
Code:
zcat

OR
Code:
gunzip -c

# 3  
Old 08-09-2011
Could you please explain?
# 4  
Old 08-09-2011
Code:
for f in *.gz; do
  if  zcat "$f" | grep -q pattern ; then
    var=`zcat "$f"`
    # do something with $var or just use the result of zcat in a pipe or do something with "$f"
done

If you have zgrep then use it - if zgrep -q pattern "$f" ; then .... If you don't have zcat use gunzip -c "$f".

Last edited by yazu; 08-09-2011 at 11:02 AM.. Reason: minor corrections
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Gunzip a file in UNIX

hi All, i have a file called rsync-3.0.9.tar.gz, i have to unzip and then uncompress it. but at first while i am trying to unzip this file, it is giving me following error: -rw-r--r-- 1 bravodba bravodba 7278458 Aug 19 08:26 rsync-3.0.9.tar.gz bash-3.2$ unzip rsync-3.0.9.tar.gz Archive: ... (12 Replies)
Discussion started by: lovelysethii
12 Replies

3. Shell Programming and Scripting

Not able to gunzip a .gz file, in ksh

i am working on unix - ksh trying to unzip a .gz file and when i executed the below, in my command prompt in ksh gunzip abc.gz it was throwing the message below: gunzip: abc.gz: unexpected end of file - pls advise what is the reason for it..i am pretty sure the .gz file had no issues. (3 Replies)
Discussion started by: billpeter3010
3 Replies

4. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

5. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

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

7. Shell Programming and Scripting

Searching for a string in .PDF files inside .RAR & .ZIP archives.

Hi, I have got a large number of .PDF files that are archived in .RAR & ZIP files in various directories and I would like to search for strings inside the PDF files. I would think you would need something that can recursively read directories, extract the .RAR/.ZIP file in memory, read the... (3 Replies)
Discussion started by: lewk
3 Replies

8. UNIX for Dummies Questions & Answers

searching files inside directory

hey, i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Discussion started by: kylethesir
4 Replies

9. UNIX for Dummies Questions & Answers

Pattern searching inside Variable - not looking at files

Hi, I've searched this site and not found this already, so if I missed on my search, sorry. I need to pass in a variable to a script, where the first three characters of that variable represent a calendar quarter, and the last 2 characters are the year. I.E. Q0105 for Q1, Q0205 for Q2, and... (3 Replies)
Discussion started by: Rediranch
3 Replies

10. UNIX for Dummies Questions & Answers

Gunzip : unexpected end of file

Hi, While uncompress , gunzip filename.cpio.gz getting the fg. error gunzip: filename.cpio.gz: unexpected end of file. Whats the problem? Size of this .gz file is 660mb . Thanks, (1 Reply)
Discussion started by: Dolly
1 Replies
Login or Register to Ask a Question