I am referring to this sample to do a unique string count. Batch file to count occurrences However, my string may include special characters eg. "orange c=US". If the string has special characters, the count won't work.
input:
[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
output:
orange c=US 3
orange c=CA 2
apple c=US 1
apple c=CA 1
code:
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