FINDSTR - find "string1" AND "string2"

Hi All,

I am writing a batch script that will search log files for two sets of strings, and I want it to encounter both before printing the line.

Here is the code:


Open in new window

At the moment, if it finds string1 (_SUBJECT) or string2 it will print the line. However I want it to only print a line if it finds string1 and string2.

Any ideas?
Who is Participating?
Bill PrewCommented:
Something like this should work, basically you find all the matches to the first string, then search that subset for the next string to match.


Open in new window

agbnielsenAuthor Commented:
Thanks very much. For bonus points that I can't actually give, is there any way I can pipe this output to a variable?
Steve KnightIT ConsultancyCommented:
Maybe that would be another question then..... You can read a line of output from another command using FOR but it depends what you want off that line, i.e. the whole line or certain parts of it.

Here is how to get the whole line.  FOR /? ... or another Q here will soon find you ways to pick out certain parts of the line:

Only oddity with the command is any special characters such as > | < need escaping with a ^ character in the for command and if in a batch file you use %% but in cmd prompt just %.

FOR /F "delims=" %%A in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do SET answer=%%A

Bill PrewCommented:
Thanks.  Steve suggested the way we typically capture command output into a variable, at least when the result is a single line.  If multiple lines could be matched then we need to take a slightly modified approach.  If you want more info on that by all means toss another question at us and link it to this one.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.