Yar
Yar

Reputation: 7457

Search through *.gz files with keeping the file name

Say I have multiple .gz files that I want to search a keyword in them. I can do this by piping zcat result to a grep like this:

zcat some.file.* | grep "keyword_1" | ... | grep "keyword_n"

The output of this command though will be just the matching line and won't have the file name in it. Is there any way I can attach the file name to the zcat output?

Upvotes: 1

Views: 8681

Answers (1)

Mike
Mike

Reputation: 5142

Try zgrep instead of zcat:

zgrep -H keyword some.file.*

And if you want to use egrep to get pattern matching:

export GREP=egrep
zgrep -H -e "(keyword1|keyword2)" some.file.*

Upvotes: 4

Related Questions