我指的是這個示例來做一個唯一的字串計數。用于計算出現次數的批處理檔案 但是,我的字串可能包含特殊字符,例如。“橙色 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
uj5u.com熱心網友回復:
只需將特殊字符=(和空格)替換為另一個計數,并將這些字符替換回輸出:
編輯:根據后文評論中的要求修改了代碼
@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
uj5u.com熱心網友回復:
這是從 Windows 10 開始可用的替代方法,它可以幫助您,(取決于您的 CSV 輸入檔案的目標欄位中存在的任何秘密“特殊字符”):
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "ifile=inputfile.csv"
Set "tfile=%TEMP%\$.log"
For /F "Tokens=4 Delims=," %%G In ('%SystemRoot%\System32\findstr.exe /I /R
/C:"^\[SUCCESS\][^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*" "%ifile%" 2^>NUL'
) Do (Echo %%G) 1>>"%tfile%"
If Not Exist "%tfile%" GoTo :EOF
For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\sort.exe /Unique "%tfile%"
') Do (SetLocal EnableDelayedExpansion & Set "}=0"
For /F Delims^=^ EOL^= %%H In ('%SystemRoot%\System32\findstr.exe /I /L /X
/C:"%%G" "%tfile%"') Do Set /A } = 1
Echo %%G,!}!& EndLocal)
Del "%tfile%"
Pause
預期輸出,(我在這里使用了逗號分隔符,因為欄位字串中不應該存在這些分隔符):
apple c=CA,1
apple c=US,1
orange c=CA,2
orange c=US,3
[編輯 /]
您的附加示例字串Grape c=US以及Grape/L c=CA從您的評論到另一個答案應該沒有困難。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322994.html
上一篇:使用批處理創建多個檔案
下一篇:QueryBuilder無效的PathExpression。必須是StateFieldPathExpression
