The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how do i pattern match a field with awk? someone123 Shell Programming and Scripting 4 06-03-2008 05:08 AM
how to use pattern match with grep rei UNIX for Dummies Questions & Answers 5 01-05-2007 12:33 AM
Match portion of the filename mpang_ Shell Programming and Scripting 1 06-27-2006 03:20 AM
pattern match and substitution, can you help? frustrated1 Shell Programming and Scripting 4 02-20-2006 04:48 AM
How to pattern match on digits and then increment? sdutto01 Shell Programming and Scripting 2 08-11-2005 11:46 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2007
Posts: 17
Stumble this Post!
To identify filename in which having match PATTERN

Hi,

Any idea to identify bunch of files( gz format) in which having match PATTERN wanted and print out those files ?


Regards,
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,303
Stumble this Post!
write a script that gunzip the files, grep for pattern and notice you if pattern is found.
  #3 (permalink)  
Old 03-21-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 145
Stumble this Post!
If you are meaning of individual .gz files, you can use zgrep

Code:
$ cat abc.txt
unix
a
unix

$ gzip.exe abc.txt

$ zgrep "unix" abc.txt.gz
unix
unix
//Jadu
  #4 (permalink)  
Old 03-21-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,303
Stumble this Post!
zgrep is not on every system... on my linux (ubuntu) server: yes. on my solaris nevada: no.

so, the answer depends on your os
  #5 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2007
Posts: 17
Stumble this Post!
If there is a single file, should be easiar. But what if I hv list of 1000 files, in which I want to identify which filelist are contains those matched pattern.

Please advise.
  #6 (permalink)  
Old 03-21-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,303
Stumble this Post!
Quote:
Originally Posted by cedrichiu View Post
If there is a single file, should be easiar. But what if I hv list of 1000 files, in which I want to identify which filelist are contains those matched pattern.

Please advise.
do it with a loop... something like:

Code:
for i in `ls -1 *.gz`
do
  #7 (permalink)  
Old 03-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
*bling* Useless Use of Glob in Backticks! Also the "ls -l" actually is a rather grave error. You could simply drop the -l, but the whole ls is redundant.

Code:
for i in *.gz
do
  gzip -dc <$i | fgrep "string" >/dev/null && echo $i
done
If you have an fgrep which supports fgrep -q, use that.

zgrep is actually fairly buggy for stuff like this; other than that, it is the proper answer. It's a fairly small shell script so you could simply find it in Google and copy to your $HOME/bin
Google The UNIX and Linux Forums
Closed Thread

Tags
linux, solaris, ubuntu

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0