2012-10-11 22 views
4

我想寫一個簡單的批處理,將循環通過文件中的每一行,如果行中包含「蘋果」或「番茄」,然後輸出該行。Windows批處理:搜索文件中的所有文件,如果行中包含「蘋果」或「番茄」回聲

我有這段代碼找到一個字符串並輸出它,但我不能在同一批次中獲得第二個字符串。我也希望它迴應找到它們的線條。

@echo OFF 

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

那就需要查找包含任一「蘋果」或「番茄」線I可以很容易地運行與上面的兩行我需要的代碼,但我需要的線被輸出間彼此。

比如我需要:

apple 
tomato 
tomato 
apple 
tomato 
apple 
apple 

NOT:提前

apple 
apple 
apple 

THEN

tomato 
tomato 
tomato 

感謝。

回答

5

Findstr已經這樣做了你:

@findstr /i "tomato apple" *.txt 

替換*.txt與通配符(和番茄蘋果與你想要的話)。

如果必須改變輸出,然後for就派上用場了:

@echo off 

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

謝謝!我不知道那很簡單,我一定是一直在努力。我如何將它找到的行設置爲一個變量,以便在輸出它之前對其進行處理? – Jsn0605

+0

哇,哈哈。< - 請不理我的回答 - 我誤解了這個問題。我也不知道那也是那麼簡單。謝謝@ ixe013 :) – kikuchiyo

+1

查看更新的答案。這是非常基本的,你會發現很多StackOverflow操作的例子。如果不是,請提出另一個問題。 – ixe013

1

我想我明白這個問題:鑑於一些線與內容Sumbitting收據diflog.txt,要提取所有這些行如果他們還含有蘋果或番茄。此外,你想要輸出蘋果線,然後是太陽線。

這是不實際的Windows電腦,我可以做測試的最好的,你可以從這裏微調,但是這可能會幫助:

@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 [email protected]@a 
    echo !line_with_apple! 
) 

echo Tomatos: 

for /f "eol=; tokens=1 delims=," %%t in ('echo !tomatos!') do (
    set [email protected]@a 
    echo !line_with_tomato! 
) 
相關問題