Grep - Search string occurrence and display directory wise count

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

We have a error log directory structure wherein we store all errors log files for a particular day in datewise directories -

errorbackup/20150629/errorlogFile3453123.log.xml
errorbackup/20150629/errorlogFile5676934.log.xml
errorbackup/20150629/errorlogFile9812387.log.xml
errorbackup/20150628/errorlogFile1097172.log.xml
errorbackup/20150628/errorlogFile1908071_log.xml
errorbackup/20150627/errorlogFile5675733.log.xml
errorbackup/20150627/errorlogFile9452344.log.xml
errorbackup/20150626/errorlogFile6363446.log.xml

I want to search for a particular string in the error log file and get the output such that I will get directory wise search result of a count of that string s occurrence. For example grep "blahblahSQLError" should output something like-

20150629:0
20150628:0
20150627:1
20150626:1

This is needed because we fixed some errors in one of the release and I want to make sure that there are no occurrences of that error since the day it was deployed to Prod. Also note that there are thousands of error log files created every day. Each error log file is created with a random number in its name to ensure uniqueness.

Answers

If you are sure the filenames of the log files will not contain any "odd" characters or newlines then something like the following should work.

for dir in errorbackup/*; do
    printf  %s:%s
  "${dir#*/}" "$(grep -l blahblahSQLError "$dir/"*.xml | wc -l)"
done

If they can have unexpected names then you would need to use multiple calls to grep and count the matching files manually I believe. Something like this.

for dir in errorbackup/*; do
    _dcount=0;
    for log in "$dir"/*.xml; do
        grep -l blahblahSQLError "$log" && _dcount=$((_dcount + 1));
    done
done

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/31123644/search-string-occurrence-and-display-directory-wise-count

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils