Every now and then I need a way to pull all of the lines from a log file that match a certain string.
An example of this would be sharepoint uls logs – every error gets a unique ID number, and the errors can cover 20-30 lines at times.
if all these lines were together, that’d be easy, but often they are not together so a grep like tool comes in handy.
Thanks to a google search and the scripthacks website, it looks like there is a command in windows for this already:
Findstr
http://scripthacks.wordpress.com/2008/09/16/grep-equivalent-for-windows-string-parsing/
Basically, if you open a command prompt and cd to the logs directory,
you can enter something like this:
Findstr “Correlation ID to find” filename
or
findstr “Correlation ID to find” *.log
which will look though all the log files in that directory (This can take forever if there are lots of large files)
Of course you can save the results by adding > c:\temp\findresults.txt to the end of the line…
A handy feature for SharePoint admins with lots of log files might be the /F:file flag
basically, this reads the file list from a file.
Why would you want this?
Because you can easily dump a list of all files to a file with this command:
DIR /B *.log > filelist.txt
once you have this list (mine was 435 lines long!) , you can easily edit it in notepad and reduce the list down to the date range you’re after, this could take you from say, 435 lines down to 24
you’d then do your search with Findstr like this:
Findstr “Correlation ID to find” /F:filelist.txt