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 ? :slight_smile:

Regards,

write a script that gunzip the files, grep for pattern and notice you if pattern is found.

If you are meaning of individual .gz files, you can use zgrep

$ cat abc.txt
unix
a
unix

$ gzip.exe abc.txt

$ zgrep "unix" abc.txt.gz
unix
unix

//Jadu

zgrep is not on every system... on my linux (ubuntu) server: yes. on my solaris nevada: no.

so, the answer depends on your os :wink:

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:

for i in `ls -1 *.gz`
do

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

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

if you would read it right... it's no an -l it's -1... so this should work!

Oops. my bad, sorry. It's still Useless because ls doesn't actually add anything, the glob is expanded by the shell before ls sees it, and ls -1 (one) will still be wrong if there are subdirectories.

jepp... so it was a point to start of for the original poster... i'm not going to write a script (even a small one) for other people. they should do the work and if they come to a point where they don't get any further, they can ask.
if you are in a direchtory and do an "ls -1 *.gz" you get every file with .gz at the end in THAT directory... so what is the problem?
if your solution works... fine. but the above works also! but let the original poster work out the solution that works for HIM.

greets,
DN2

If there are directories matching the wildcard, that is not what you will get. It's a fairly academic point in this particular context, but why not give hints which are correct regardless of circumstances.

At the risk of hubris, I suggest you google for "useless use of backticks".

i think my hint wasn't uncorrect... maybe your way ist better/nicer/other... whatever. my solution works and yours works also and is better code. :b:
but which directory matches the wildcard *.gz?

nice one. added it to my bookmarks.

It depends.

mkdir "i can name my directories whatever I like (*:".gz

jepp... i see something like that all the time.

I don't think that this discussion is helping the OP, so I'll close the thread.