我正在尝试编写一个简单的批处理,它将遍历文件中的每一行,如果该行包含“apple”或"tomato“,则输出该行。
我有这个代码来找到一个字符串并输出它,但是我不能在同一批中获得第二个字符串。我还希望它在找到它们时回显这些行。
@echo OFF
for /f "delims=" %%J in ('findstr /ilc:"apple" "test.txt"') do (
echo %%J
)它将需要找到包含“苹果”或“番茄”的行我可以很容易地运行上面的代码与我需要的两行,但我需要这两行是相互输出的。
例如,我需要:
apple
tomato
tomato
apple
tomato
apple
apple不是:
apple
apple
apple然后
tomato
tomato
tomato提前谢谢。
发布于 2012-10-12 00:01:13
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发布于 2012-10-12 00:14:18
我想我理解问题所在:给定diflog.txt中包含内容总和收据的行,如果行中还包含苹果或西红柿,则需要提取所有这些行。此外,您希望一起输出apple行,然后输出toomato行。
这是我在没有实际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!
)https://stackoverflow.com/questions/12843731
复制相似问题