Help:Another Findstr Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kupaloid
Posts: 4
Joined: 06 Sep 2009 07:42

Help:Another Findstr Question

#1 Post by kupaloid » 06 Sep 2009 08:19

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

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Finding ip addresses in firewall config files

#2 Post by SenHu » 06 Sep 2009 09:49

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.

Code: Select all

script "C:/Scripts/findip.txt"



Make sure you change the "C:/folder" to the correct folder path before trying.


Sen

kupaloid
Posts: 4
Joined: 06 Sep 2009 07:42

Finding ip addresses in firewall config files

#3 Post by kupaloid » 06 Sep 2009 19:15

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

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Listing only the files that contain the ip address

#4 Post by SenHu » 07 Sep 2009 10:51

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

Code: Select all

echo "File:\t" $file


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

kupaloid
Posts: 4
Joined: 06 Sep 2009 07:42

#5 Post by kupaloid » 08 Sep 2009 04:13

Hi Sen, thanks again. Script working perfectly. Please let me know once you have created a batch file.

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

#6 Post by avery_larry » 08 Sep 2009 12:22

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

kupaloid
Posts: 4
Joined: 06 Sep 2009 07:42

#7 Post by kupaloid » 09 Sep 2009 03:38

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

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

#8 Post by avery_larry » 09 Sep 2009 14:50

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

Post Reply