Windows Batch : Search all files in file, 如果行包含 "apple"或 "tomato"回显它

标签 windows batch-file cmd findstr

我正在尝试编写一个简单的批处理,它将遍历文件中的每一行,如果该行包含“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 替换为您的通配符(将 tomato apple 替换为您想要的词)。

如果你必须改变输出,那么 for 就派上用场了:

@echo off

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

关于Windows Batch : Search all files in file, 如果行包含 "apple"或 "tomato"回显它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843731/

相关文章:

c - 显示存储在 window dc 中的位图

windows - 在 Windows Batch 中创建列表或数组

python - 从 Python 脚本启动 VirtualBox VM

batch-file - 在打开 Internet Explorer 选项卡之前运行 tomcat

mysql - 如何在cmd Windows中运行Mysql 5.7

windows - windows和linux在网络编程上有多少不同?

windows - 为什么我的整个批处理脚本作为 SETLOCAL 命令运行?

windows - 包括来自 "for delims"的特殊字符 (&) - 批处理文件中的结果

windows - 如何在 DB2 表上写入数据阶段性能统计信息?

batch-file - 如何使用cmd打开URL?