Tag Archives: logs

grep lines having a text string from compressed files

The log of an application in my job is archived in a file, 1 file per day. I was requested to extract the activity of one client from these logs for the whole August, i.e., from 31 compressed files.

I placed all compressed log files into a folder called /database and I created a file zgrep_all.sh to search and extract the log lines of my client. (If your *.gz  files are located somewhere else then pay attention to line 11 of tje script and modify it properly)

#!/bin/bash

# author: tomeu.mir
# filename: zgrep_all.sh
# desc  : search string in all *gz files and spools the output to a file
# usage : . zgrep_all.sh <search string> <output filename>
#   
search_string=$1
output_file=$2

for f in database/*.gz
do
  now=$(date +”%Y/%m/%d %T”)
  echo “[$now] Processing $f file with search string $search_string and output file $output_file”   
  zgrep -i “$search_string” $f >>  $output_file
done
now=$(date +”%Y/%m/%d %T”)
echo “[$now] done.”

Finally I executed the following:

$ . zgrep_all.sh MYCLIENTNAME output.log

Advertisements