@ECHO OFF
SETLOCAL
FOR %%i IN (mir2 raid5 raid6) DO SET /a %%i=0
FOR /f "delims=" %%i IN (file1.txt) DO CALL :process %%i
ECHO 2-Way Mir : %mir2%
ECHO RAID-5 : %raid5%
ECHO RAID-6 : %raid6%
GOTO :EOF
:process
SET "line=%*"
:loop
SET value=%2
IF DEFINED value shift&GOTO loop
SET line=%line:~38,9%
IF /i "%line%"=="2-way mir" SET /a mir2+=%1
IF /i "%line%"=="RAID-5 " SET /a raid5+=%1
IF /i "%line%"=="RAID-6 " SET /a raid6+=%1
GOTO :EOF
应该为你做这项工作。我假设密钥字符串周围的星号是使这些字符串变粗的失败尝试。我还假设数据是固定列格式。实际上,如果这是真的,可能有一种更简单的方法:
@ECHO OFF
SETLOCAL
FOR %%i IN (mir2 raid5 raid6) DO SET /a %%i=0
FOR /f "delims=" %%i IN (file1.txt) DO CALL :process %%i
ECHO 2-Way Mir : %mir2%
ECHO RAID-5 : %raid5%
ECHO RAID-6 : %raid6%
GOTO :EOF
:process
SET "line=%*"
IF /i "%line:~38,9%"=="2-way mir" SET /a mir2+=%line:~70%
IF /i "%line:~38,9%"=="RAID-5 " SET /a raid5+=%line:~70%
IF /i "%line:~38,9%"=="RAID-6 " SET /a raid6+=%line:~70%
GOTO :EOF
如果需要,可以进一步简化。请注意,语句中的长度9
必须匹配,并且不区分大小写。if
/i
if
扩展匹配 - 稍微复杂一点,但会自动调整......
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
SET maps="2-way mir" "RAID-5 "
SET maps=%maps% "RAID-6 "
SET strcnt=0
FOR %%i IN (%maps%) DO CALL :setup %%i
FOR /f "delims=" %%i IN (file1.txt) DO CALL :process %%i
SET mapnbr=1
:ploop
CALL ECHO %%$_%mapnbr%%% : %%$%mapnbr%%%
SET /a mapnbr+=1
IF %mapnbr% leq %strcnt% GOTO ploop
GOTO :EOF
:process
SET "line=%*"
SET mapnbr=%strcnt%
:matchloop
CALL SET match=%%$_%mapnbr%%%
IF /i "%line:~38,9%"==%match% CALL SET /a $%mapnbr%+=%line:~70%&GOTO :eof
SET /a mapnbr-=1
IF %mapnbr% neq 0 GOTO matchloop
GOTO :EOF
:: Set $n=0 & $_n=string-to-match
:setup
SET /a strcnt+=1
SET /a $%strcnt%=0
SET $_%strcnt%=%1
GOTO :eof
本质上,变量$n
包含计数,$_n
要在引号中匹配的字符串。只需将更多带引号的 9 字符字符串添加到maps
空格或逗号分隔符中。