Windows package: search for all files in a file, if the line contains "apple" or "tomato", echo it

I am trying to write a simple batch that will go through each line in the file, and if the line contains "apples" or "tomato", then to output this line.

I have this code to find one line and print it, but I can not get the second in the same batch. I also want him to repeat the lines on which they found them.

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

He will need to find strings containing either "apples" or "tomatoes." I can easily run the code above using the two lines that I need, but I need the lines that are displayed among themselves.

For example, I need:

 apple tomato tomato apple tomato apple apple 

NOT

 apple apple apple 

THEN

 tomato tomato tomato 

Thanks in advance.

+4
source share
2 answers

Findstr already does this for you:

 @findstr /i "tomato apple" *.txt 

Replace *.txt with your template (and the 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 
+5
source

I think I understand the problem. Given some line in the diflog.txt file with the contents of the Sumbitting Receipt, you want to extract all such lines if they also contain an apple or tomato. Also, you want to output apple lines together and then toomato lines.

This is the best I can do without a real Windows computer for testing, and you can configure it here, but it can help:

 @echo OFF setlocal enabledelayedexpansion set apples= set tomatos= for /f "delims=" %%l in ('findstr /ilc:"Submitting Receipt" "diflog.txt"') do ( set line=%%l for /f "eol=; tokens=1 delims=" %%s in ('echo !line! ^| findstr /ic:"apple"') do ( set new_apple=%%s set apples=!apples!,!new_apple! ) for /f "eol=; tokens=1 delims=" %%s in ('echo !line! ^| findstr /ic:"tomato"') do ( set new_tomato=%%s set tomatos=!tomatos!,!new_tomato! ) ) echo Apples: for /f "eol=; tokens=1 delims=," %%a in ('echo !apples!') do ( set line_with_apple=@ @a echo !line_with_apple! ) echo Tomatos: for /f "eol=; tokens=1 delims=," %%t in ('echo !tomatos!') do ( set line_with_tomato=@ @a echo !line_with_tomato! ) 
+1
source

Source: https://habr.com/ru/post/1439205/


All Articles