Command line to copy matching lines of text out of log files
You've been given a directory of log files, one for each day for the last twenty-four months, and you need to find the lines that contain the IP address 192.168.0.45 and place them in a single text file for further analysis.
One caveat: You have a Windows machine, you can't install Perl, or maybe you don't have time, or maybe you're here because you're like me and not a native Linux or Windows command line hacker but know how to Google the commands you need to just get your work done.
Yes, for most of us on this planet, that is ONE caveat.
You Googled your situation and found the answer on Stack Overflow. Good news! There's a Windows command for that!
After adapting for your unique situation, you came up with this:
type "\*.log" | find.exe "192.168.0.45" > "myoutput.txt"
Whew! Stack Overflow to the rescue again!
And now you've become a whiz so your boss has asked you to go through more files. CSVs, text records, etc. Oh, and you will need to do this on a routine basis.
Crap.
Well, there's a batch script for that (overly commented for ease of use/understanding, of course!):
@echo OFF
rem === COPY MATCHING LINES OUT OF TEXT/LOG FILES =====
rem \*
rem \* Chad Leigh Kluck, chadkluck.net
rem \* May 12, 2016
rem \*
rem \* Originally developed for finding accessed databases in a log file
rem \*
rem \* Can be used with any plain text log/record type file(s) where you need to
rem \* quickly copy out relevant lines of text
rem \*
rem \* You can search through a single file, or multiple files at once
rem \*
rem \* Based on post at:
rem \* http://stackoverflow.com/questions/31608362/search-a-string-in-a-line-in-a-text-file-and-copy-it-to-another-text-file-or-a-n
rem \*
rem \* USAGE:
rem \*
rem \* Set the three variables in the sections below:
rem \*
rem ============== WORDS TO SEARCH FOR ================
rem \*
rem \* Separate multiple with space. Each word is an OR
rem \*
rem \* FOR EXAMPLE: To only copy lines that contain 127.0 or localhost
rem \* =127.0 localhost
rem \*
set searchWords=MediaServer
rem ============== FILE TO SEARCH IN ==================
rem \*
rem \* Set the file name to search through or use \* to
rem \* combine multiple files
rem \*
rem \* FOR EXAMPLE: input.text for a single file
rem \* \*.log for all .log files in dir
rem \* 2016\*.log for all .log files beginning with 2016
rem \*
set searchFile=database.log
rem ============== FILE TO OUTPUT TO ==================
set outputFile=output.txt
rem ============== SEND TEXT TO THE USER ==============
echo.
echo Going through the file: %searchFile%
echo and Finding the word(s): %searchWords%
echo and Outputing to file: %outputFile%
echo.
rem ============ THE SINGLE COMMAND THAT DOES IT ALL! =
type "%searchFile%" | %SystemRoot%\\System32\\find.exe "%searchWords%" > "%outputFile%"
rem ============= LET USER KNOW WE'RE DONE ============
echo Done
echo.
pause
Have fun!