grep on both zip and non zip files

gudii9
gudii9 used Ask the Experts™
on
when i do ls -ltr i see bunch of .gz files says 123.gz and 456.gz
and bunch of no gz files say 789.log etc

how to grep on say "testing" on both zip gz files and non zip files

grep "testing" 789.log

zgrep "testing"  123.gz

not sure how to combine above two together

also how search today log between 10 am to 11 am for that "testing" word in log?
please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can just use zgrep for both. zgrep will search plain text as well as compressed files
zgrep "testing" 123.gz 789.log
zgrep "testing" *.gz *.log
David FavorFractional CTO
Distinguished Expert 2018

Commented:
If you're searching may text + .gzip files, you can always do this in your shell startup file...

alias grep=zgrep

Open in new window

Author

Commented:
zgrep "testing" 123.gz 789.log
zgrep "testing" *.gz *.log

in the result how to know whether it found in .gz file or .log file

please advise
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

The search term shows up as

123.gz: Filename followed by a colon, then whatever the line is here that includes your search term "testing"
789.log: another line with the word testing in it.

You should probably just run the command to see what you get.

Author

Commented:
123.gz

has 10 different files(file1,file2...file10) which it list one of that file rather than saying it as 123.gz
some kind of relative path would have helped like
found testing at line 159
123.gz/file2
etc
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Seems like your original question has been answered.

Go ahead + close this question + open another question related to your last update. Also provide more detail, as your last update requires much more detail for an answer.
I don't believe gzip can make an archive of 10 different files without using tar. Usually it is named tar.gz in that case.

If it is a tar archive, then you can view the files inside of it.

tar -tf 123.gz

If someone took ten files and concatenated them into a gzip file, then sorry the filenames are lost.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial