Page 1 of 1

findstr question

Posted: 07 Jul 2009 21:47
by mungurk
Hello all,
I am hoping for any advice on the findstr command, for the following scenario

I have a directory containing about 900 log files, far too many to check manually, x:\files\logs\pro

I am trying to complete a Batch Script that searches for the presence of the string "sorry.google" as part of the text in the log file.

An individual log file could potentially contain 0 or 100s of instances of the "sorry.google" string.

The hope is to produce a report containing the frequency of the string "sorry.google" followed by a hyphen, and the file name for each log file in the directory.

For instance:
11 - google.log
37 - yahoo.log

I am sure I will need the findstr command, and the /i flag.
I also think the "/d" is needed

Any advice?

thanks!

Posted: 08 Jul 2009 10:48
by avery_larry
Well, a for loop to get the count of each file separately.

find /c will give you the count of the instances in a file. So something like this . . :

Code: Select all

@echo off
set "logdir=x:\files\logs\pro"
set "reportfile=x:\files\logs\report.txt"
set "strng=sorry.google"
del "%reportfile%" >nul 2>nul
cd /d "%logdir%"

for %%a in (*) do (
   for /f "usebackq" %%b in (`type "%%~a" ^|find /i /c "%strng%"`) do echo.%%b - %%~a >>"%reportfile%"
)

This will return the number of LINES that have sorry.google, not the actual number of occurances of sorry.google (if a single line happens to have multiple instances, it will only add 1 for that entire line).

thank you for helping me with the batch

Posted: 08 Jul 2009 19:04
by mungurk
avery_larry,
It sure looks like I have a lot more to learn about scripting!
This works beautifully.
I'll study this one, and try to adapt it to different scenarios.

May you live a thousand years in good health!

- mungurk

Posted: 08 Jul 2009 21:05
by ghostmachine4
if you can download and usegawk for windows, here's a one liner

Code: Select all

C:\test>gawk "/sorry.google/{f[FILENAME]++}END{for(i in f) print f[i],i}" logs*txt