如何从文本文件中找到相同的特定单词并将其作为输出?

时间:2017-08-26 17:30:06

标签: batch-file cmd

我有一个名为file.txt的文本文件,其中包含

A: - No such Root Directory
B: - No such Root Directory
C: - Fixed Drive
D: - Fixed Drive
E: - Removable Drive
F: - CD-ROM Drive
G: - Removable Drive
H: - No such Root Directory
I: - No such Root Directory
J: - No such Root Directory
K: - No such Root Directory
M: - No such Root Directory
N: - No such Root Directory
O: - No such Root Directory
P: - No such Root Directory
Q: - No such Root Directory
R: - No such Root Directory
S: - No such Root Directory
T: - No such Root Directory
U: - No such Root Directory
V: - No such Root Directory
W: - No such Root Directory
X: - No such Root Directory
Y: - No such Root Directory
Z: - No such Root Directory

在此文件中,您可以注意到第5行和第7行中出现两次Removable字样。

如果它具有相同的特定单词Removable两次或三次,则需要显示输出Removable word found multiple times

如果它没有相同的特定字Removable两次或三次,则需要显示输出There is no multiple Removable word found

对于此输出,我运行了以下程序,

findstr /b Removable "file.txt" do ( 
echo Removable word found multiple times 
) || ( 
echo There is no multiple Removable word found 
)
pause

我知道错了。但是,我尽我所能。请纠正我的朋友。这对我很有帮助。

1 个答案:

答案 0 :(得分:2)

您应该能够使用Find /I /C "Removable"<"file.txt"来获取该文件中的Removable实例数。然后你需要做的就是检查那个号码。最常见的方法是通过将该命令放在for循环中来读取该值。

与命令提示符窗口中的这一行一样:

For /F %A In ('Find /I /C "Removable"^<"file.txt"') Do @If %A Lss 1 (Echo=There is no multiple Removable word found) Else Echo Removable word found %A times