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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 PrewIT / Software Engineering ConsultantCommented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.