Solved

String search in multiple gziped files

Posted on 2004-10-14
7
1,111 Views
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!

0
Comment
Question by:theoradically
7 Comments
 
LVL 14

Accepted Solution

by:
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"
      done
done
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12312025
zgrep
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 12312137
or possibly gzgrep depending on what Unix flavour you are on.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

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

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

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
do
    gzcat "$FN" | grep -q Error
    if [ $? -eq 0 ]
    then
        echo "${FN}:"
        gzcat "$FN" | grep -i Error
        echo ""
    fi
done

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

Expert Comment

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

Author Comment

by:theoradically
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


BYW,

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

Thanks for your input
0
 
LVL 51

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now