2017-08-26 53 views
0

我有一個名爲的文本文件,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 

在這個文件中,你可以看到詞Removable出現兩次在第5行和第7行

如果有相同的特定的詞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 

我知道它錯了。但是,我盡力了。請糾正我的朋友。它會對我很有幫助。

回答

2

您應該可以使用Find /I /C "Removable"<"file.txt"來獲取該文件中的可移動實例的數量。然後,你需要做的就是檢查該號碼。這樣做的最常見方法是通過將該命令放在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 
相關問題