Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I find all the files that contains two strings?

My problem is to create a batch script file for Windows and iterate through a lot of files and find every file which has a line that contains two specified strings. So if the whole file contains those strings, that's not good enough, they should be at the same line.

For example, I have 5 files which contains the following:

1st: apple:green
2nd: apple
green
3rd: green
apple
4th: apple: yellowgreen
5th: apple: green

It should return the filenames of the first, fourth and fifth file.

Here is what I have:

FINDSTR /s /i /m "apple green" *.txt | FINDSTR "\MyDirectory" >> results.txt

How should I modify this to make it work?

like image 290
fzl Avatar asked Dec 21 '25 18:12

fzl


2 Answers

findstr /i /s /m /r /c:"apple.*green" /c:"green.*apple" *.txt
like image 156
MC ND Avatar answered Dec 23 '25 13:12

MC ND


EDITED TO WORK WITH FINDSTR

This regex worked for me: "apple.*green green.*apple"

Also, your write to file command with the pipe did not work for me (perhaps I'm missing something). If it doesn't work for you, perhaps this will:

FINDSTR /s /i /m "apple.*green green.*apple" *.txt >> results.txt

like image 33
OzW Avatar answered Dec 23 '25 12:12

OzW