findstr question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mungurk
Posts: 4
Joined: 31 May 2009 23:43

findstr question

#1 Post by mungurk » 07 Jul 2009 21:47

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!

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 08 Jul 2009 10:48

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).

mungurk
Posts: 4
Joined: 31 May 2009 23:43

thank you for helping me with the batch

#3 Post by mungurk » 08 Jul 2009 19:04

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#4 Post by ghostmachine4 » 08 Jul 2009 21:05

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

Post Reply