Page 1 of 1
Help:Another Findstr Question
Posted: 06 Sep 2009 08:19
by kupaloid
Hello All, I am new here and not really good at batch file. Hope you could help me for the following scenarios:
1. I have a folder containing text files of the config of each firewall.
2. I would like to search the folder with the list of IP address that I have. Filename is list.txt.
3. I know I could do this in findstr but I'm having problem on the output that I want, so far this is what I have:
Code: Select all
@Echo off
if exist results.txt del results.txt
findstr /m /g:list.txt *.* > results.txt
if %errorlevel%==0 (
echo Found! logged files into results.txt
) else (
echo No matches found
)
:: DONE
4. This only gives me the filename that contains the match (/m) so I don't know w/c IP matches w/c filename.
5. I want an output that will list each IP address first and below it the filenames that contains the IP.
Thanks for your help
Finding ip addresses in firewall config files
Posted: 06 Sep 2009 09:49
by SenHu
In some folder C:/folder, you have *.txt files. In file C:/folder/list.txt you have a list of strings, one per line. (They are IP addresses, but doesn't really matter.) You want to list all lines from all *.txt files that contain an IP address (string) from the list.
Code: Select all
# Script findip.txt
# Change dir to C:/folder.
cd "C:/folder"
# Get the search string list into a string variable.
var str list ; cat "list.txt" > $list
# Go thru search strings one by one.
var str onestr ; lex "1" $list > $onestr
while ($onestr <> "")
do
# List files names, line numbers and line contents of all
# lines in all *.txt files that contain this search string.
script "SS_FindStr.txt" files("*.txt") str($onestr)
# Get the next search string.
lex "1" $list > $onestr
done
Script is in biterscripting (
http://www.biterscripting.com ). To try, save the abve script as C:/Scripts/findip.txt, enter the following command in biterscripting.
Make sure you change the "C:/folder" to the correct folder path before trying.
Sen
Finding ip addresses in firewall config files
Posted: 06 Sep 2009 19:15
by kupaloid
Hi Sen, thanks for your help but I'm hoping some batch files that would not require installation of 3rd party software since we do have restricted access to our workstations.
I have tried this though on my home desktop and works like a charm. One thing though, can we not list the filename that doesn't have a match? thanks
Listing only the files that contain the ip address
Posted: 07 Sep 2009 10:51
by SenHu
Hi kupaloid
Let me think about a batch script for you.
One thing though, can we not list the filename that doesn't have a match?
Yes. Make a copy of script C:/Scripts/SS_FindStr.txt to C:/Scripts/findstr.txt. In the findstr.txt script, change the line
to
Code: Select all
var str content
cat $file > $content
if ( { sen ("^"+$str+"^") $content } > 0 )
echo "File:\t" $file
endif
Then in script findip.txt, change the line
Code: Select all
script "SS_FindStr.txt" files("*.txt") str($onestr)
to
Code: Select all
script "findstr.txt" files("*.txt") str($onestr)
We are basically changing their sample script SS_FindStr.txt to suit our requirements.
Sen
Posted: 08 Sep 2009 04:13
by kupaloid
Hi Sen, thanks again. Script working perfectly. Please let me know once you have created a batch file.
Posted: 08 Sep 2009 12:22
by avery_larry
"Pure" batch code:
*UNTESTED*
Code: Select all
@echo off
del results.txt>nul 2>nul
for %%a in (*) do (
findstr /m /g:list.txt "%%~a" >nul 2>nul
if not errorlevel 1 set found=1&&echo."%%~a">>results.txt
)
if defined found (
echo Found! logged files into results.txt
) else (
echo No matches found.
)
Posted: 09 Sep 2009 03:38
by kupaloid
Hi avery_larry, thanks for the code but this only gives me the filename that contains the match so I don't know w/c IP matches w/c filename. I was hoping output would be like this:
Code: Select all
"If there is a match"
1. <1st string>
-filename 1
-etc
2. <2nd string>
-filename 1
-filename 2
-etc
"If no match"
3. <3rd string>
-no match found
etc.
Thanks again and looking forward for your help
Posted: 09 Sep 2009 14:50
by avery_larry
You can simply remove the:
>nul 2>nul
to get the output of the findstr string as well as the filename. This will put the output first, and the filename second.
OR if you want the filename first and the output second:
*untested*
Code: Select all
@echo off
del results.txt>nul 2>nul
for %%a in (*) do (
findstr /m /g:list.txt "%%~a" >nul 2>nul
if not errorlevel 1 (
set found=1
echo.Match found in "%%~a">>results.txt
findstr /m /g:list.txt "%%~a">>results.txt
echo.>>results.txt
echo.>>results.txt
) else (
echo No match found in "%%~a">>results.txt
)
)
if defined found (
echo Found! logged files into results.txt
) else (
echo No matches found.
)