的列表中的每個字符串我有一個包含一個低於其他的員工代碼列表的文本文件:批處理腳本來計數文件
KUMAA286
KUMAA286
MATCHS02
GANNAS
MATCHS02
GANNAS
我想一個批處理腳本,可以給我一個計數每個僱員代碼在一個output.txt
文件中。
E.摹:
The number of KUMAA286 is 2
The number of MATCHS02 is 2
The number of GANNAS is 2
的列表中的每個字符串我有一個包含一個低於其他的員工代碼列表的文本文件:批處理腳本來計數文件
KUMAA286
KUMAA286
MATCHS02
GANNAS
MATCHS02
GANNAS
我想一個批處理腳本,可以給我一個計數每個僱員代碼在一個output.txt
文件中。
E.摹:
The number of KUMAA286 is 2
The number of MATCHS02 is 2
The number of GANNAS is 2
簡單:
@echo off
setlocal
rem Read the strings and count each one on its corresponding array element
for /F %%a in (input.txt) do set /A "count[%%a]+=1"
rem Show the results
for /F "tokens=2,3 delims=[]=" %%a in ('set count[') do echo The number of %%a is %%b
同意,即使缺少初始化。更簡單的話題Powershell one liner:'gc。\ Employees.txt | group |%{「{0}的數目是{1}」-f $ _。Name,$ _。Count}' – LotPings
自相矛盾;-)
你必須事先與前一個
每個條目進行比較,如果相同數量高達排序列表。
如果輸出計數和最後輸入不相同。
有不同的數,這個變化表:
KUMAA286
GANNAS
KUMAA286
MATCHS02
GANNAS
GANNAS
而且這批
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set Cnt=-1
Set Last=n/a
For /f %%A in (' Sort Employees.txt') Do (
if %%A neq !Last! (
if !Cnt! geq 1 Echo The number of !Last! is !Cnt!
Set Cnt=1
Set Last=%%A
) Else (
Set /A Cnt+=1
)
)
If %Cnt% geq 1 Echo The number of !Last! is !Cnt!
返回結果:
The number of GANNAS is 3
The number of KUMAA286 is 2
The number of MATCHS02 is 1
代碼工作正常。謝謝你的幫助 –
我想了解,你能解釋什麼是Set Last = n/a,什麼是不適用 –
這只是爲了讓var定義任何內容。否則,當第一行中的「!Last!」計算爲無效時,第一個if會產生無效比較。 'n/a'通常代表不可用。 – LotPings
你應該顯示自己的編碼工作,[SO]不是腳本編寫服務。 – LotPings