首页 > 解决方案 > 如何在窗口bat文件中计算具有特殊字符的唯一字符串

问题描述

我指的是这个样本来做一个唯一的字符串计数。用于计数出现次数的批处理文件 但是,我的字符串可能包含特殊字符,例如。“橙色 c = 美国”。如果字符串有特殊字符,计数将不起作用。

输入:

[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,apple c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,apple c=CA,xxxx,xxxx

输出:

orange c=US  3
orange c=CA  2
apple c=US 1
apple c=CA 1

代码:

set "file=test.out.log"
(
for /f "tokens=1,2,3,4,5,6,7 delims=," %%a in ('findstr /I /N /C:"[SUCCESS]" "%file%"') do (
    set "t=%%d" <--- %%d will extract "orange c=US"
    call :handleType
)

rem Enumerate find types and echo type and number of occurrences
rem The inner loop is to allow underscores inside type
for /f "tokens=1,* delims=_" %%a in ('set _type_ 2^>nul') do (
    for /f "tokens=1,2 delims==" %%v in ("%%b") do (
      echo %%v %%w
    )
)) > output.txt

rem Clean and exit
endlocal
exit /b
pause > nul
:handleType
  rem %t% ----> orange c=US
  set "t=%t:'=%"

for /f "tokens=*" %%t in ("%t:"=%") do (
    set /a "_type_%%~t+=1"
)
goto :EOF

标签: batch-filewindow

解决方案


只需将特殊字符=(和空格)替换为另一个用于计数,然后将这些字符替换回输出:

编辑根据后面评论中的要求修改代码

@echo off
setlocal EnableDelayedExpansion

rem Count items
for /F "tokens=4 delims=," %%d in ('findstr /I /L "[SUCCESS]" test.txt') do (
   set "item=%%d"

   rem Replace special characters
   for %%a in ("+=PLUS" "/=SLASH") do (
      for /F "tokens=1,2 delims==" %%b in (%%a) do set "item=!item:%%b=%%c!"
   )

   rem Separate on SPace and equal-sign characters
   for /F "tokens=1,2,3 delims== " %%x in ("!item!") do (
      set /A "count[%%x_%%y_%%z]+=1"
   )
)

REM set count[

rem Show counts
for /F "tokens=2-5 delims=[_]=" %%a in ('set count[') do (

   rem Replace back special characters
   set "item=%%a"
   for %%a in ("PLUS=+" "SLASH=/") do (
      for /F "tokens=1,2 delims==" %%b in (%%a) do set "item=!item:%%b=%%c!"
   )

   echo !item! %%b=%%c  %%d
)

输入:

[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,orange c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,apple c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,apple c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,Grape+ c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,Grape+ c=CA,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,Grape/L c=US,xxxx,xxxx
[SUCCESS] xxxx,xxxx,xxxx,Grape/L c=CA,xxxx,xxxx

输出:

apple c=CA  1
apple c=US  1
Grape+ c=CA  1
Grape+ c=US  1
Grape/L c=CA  1
Grape/L c=US  1
orange c=CA  2
orange c=US  3

推荐阅读