Saturday, May 4, 2013

Script to search in gzipped file

Many times we have to do search in gzipped files in Unix. For that generally we follow a very simple approach ,simply  gunzip all the files and search the content through grep command. But this simple approach will work only if the number of gzipped files is very less. What happens when the number of gzipped files are 30000 and more. Then the above steps fails and we have to use gzcat command the grep the command from the gzipped file without gunzipping the files. But there is a problem that while using the grep command with gzcat , we are not able to know in which file the content is present.

Ex:-

   gzcat *.gz|grep abc

the above command will not show file name where the "abc" is present.

For that I have a simple script, that will show the name of the file in which it is present.

for i in *.gz
do
a=`gzcat $i |grep abc |wc -l`
if [ $a != 0 ]

then
echo $i
fi
done


You can use the above script , by modifying according to you requirement