繁体   English   中英

Windows批处理:搜索文件中的所有文件,如果行包含“ apple”或“ tomato”,则将其回显

[英]Windows Batch: Search all files in file, if line contains “apple” or “tomato” echo it

我正在尝试编写一个简单的批处理,该批处理将遍历文件中的每一行,如果该行包含“ apples”或“ tomato”,则输出该行。

我有这段代码来找到一个字符串并将其输出,但是我无法在同一批次中获得第二个字符串。 我也希望它在找到它们时回显这些线条。

@echo OFF

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

它需要找到包含“ apples”或“ tomato”的行,我可以轻松地用我需要的两行运行上面的代码,但是我需要将这些行相互输出。

例如,我需要:

apple
tomato
tomato
apple
tomato
apple
apple

不:

apple
apple
apple

然后

tomato
tomato
tomato

提前致谢。

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

我想我理解问题所在:在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 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!
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM