String search in multiple gziped files

Posted on 2004-10-14
Last Modified: 2012-06-21
I looked through the previous solutions, but did not find this specific anwer.

When I grep for a string in a list of regular files, it shows what file the string is in when it lists the occurances of the string.

I realize to do a string search in a directory of gzipped files, I can

gzcat * | grep -i Error

But, this only shows the occurance of the string and does not show which files the string occurs in.  So, I have to do the search manually against each file in the directory.  What do I do to have the output of  " gzcat * | grep -i Error "  show which files it finds the string.  Thanks for your help!

Question by:theoradically
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Accepted Solution

chris_calabrese earned 25 total points
ID: 12311907
You want something along the lines of

ls *.gz | while read file
do  gzcat "$file" | grep -i Error | while read line
      do echo "$file: $line"
LVL 51

Expert Comment

ID: 12312025
LVL 48

Assisted Solution

Tintin earned 100 total points
ID: 12312137
or possibly gzgrep depending on what Unix flavour you are on.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12324823
An alternate that will print the file name above the lines that match:

echo *.gz | while read FN
    echo "${FN}:"
    gzcat "$FN" | grep -i Error
    echo ""

This will print every file name. If you want to only see the file names that contain the search then do something like this:

echo *.gz | while read FN
    gzcat "$FN" | grep -q Error
    if [ $? -eq 0 ]
        echo "${FN}:"
        gzcat "$FN" | grep -i Error
        echo ""

This will be a little slower because it does 2 greps.
LVL 51

Expert Comment

ID: 12331591
re-enventing the wheel :-(
if there's a zcat, there's zgrep too

Author Comment

ID: 12340105
gzgrep is what I was looking for - thanks tintin
gzgrep -i Error * - gives the filename and occurance of the string within it

Chris - your script worked , but was a little more than I was looking for


when I do      which zcat  -   I get ... /bin/zcat
when I do      which zgrep -  I get    -   no zgrep in .....

Thanks for your input
LVL 51

Expert Comment

ID: 12340650
if the name is zgrep or gzgrep depends on your installation, rpm, make, whatever ...
anyway, you found what you need

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question