Windows Batch: Search all files in file, if line contains "apple" or "tomato" echo it

Jsn0605 picture Jsn0605 · Oct 11, 2012 · Viewed 10.1k times · Source

I'm trying to write a simple batch that will loop through every line in a file and if the line contains "apples" or "tomato" then to output that line.

I have this code to find one string and output it but I can't get the second in the same batch. I also want it to echo the lines at it finds them.

@echo OFF

for /f "delims=" %%J in ('findstr /ilc:"apple" "test.txt"') do (
echo %%J
)

It would need to find lines that contain either "apples" or "tomato" I can easily run the code above with the two lines I need but I need the lines to be outputted inter each other.

For example I need:

apple
tomato
tomato
apple
tomato
apple
apple

NOT:

apple
apple
apple

THEN

tomato
tomato
tomato

Thanks in advance.

Answer

ixe013 picture ixe013 · Oct 11, 2012

Findstr already does this for you :

@findstr /i "tomato apple" *.txt

Replace *.txt with your wildcard (and tomato apple with the words you want).

If you must change the output, then for comes in handy :

@echo off

for /f %%i in ('findstr /i "tomato apple" *.txt') do @echo I just found a %%i