I would like write a batch file to count the number of occurrences of a specific character in each line of a text file.
For example, the count of \
in the string "aa\bb\cc\dd\"
would be 4.
The find
and the findstr
show only the number of lines which is contains the exact character.
You might try the following script, providing the input string as (quoted) command line argument:
set "STRING=%~1$"
set STRING="%STRING:\=" "%"
set /A "COUNT=-1"
for %%E in (%STRING%) do set /A "COUNT+=1"
echo Count of `\`: %COUNT%
This replaces every character to be counted by "
+ SPACE + "
and encloses the entire string in between ""
, so the input string aa\bb\cc\dd\
becomes "aa" "bb" "cc" "dd" ""
. The resulting string is fed into a for
loop that recognises individual items to iterate through -- five in this case. The counter variable COUNT
is initialised with a value of -1
, so the result is not the number of iterated items but the separators, namely the \
characters present in the original string.
This approach fails if the string contains ?
or *
characters. It would also fail in case the character to count is one of the following: "
, %
, =
, *
, ~
.
@echo off
setlocal
set "string=aa\bb\cc\dd\"
set "count=-1"
for %%a in ("%string:\=" "%") do set /A count+=1
echo %count%
This method works correctly as long as the string don't include wild-card characters: *?
; if this is required, I would use the same npocmaka's method, but written in a simpler way:
@echo off
setlocal EnableDelayedExpansion
set "string=aa\bb\cc\dd\"
set "str=A%string%Z"
set "count=-1"
for /F "delims=" %%a in (^"!str:\^=^"^
% Do NOT remove this line %
^"!^") do (
set /A count+=1
)
echo %count%
While slow, you can try with this
@echo off
setlocal enableextensions disabledelayedexpansion
set "inputFile=input.txt"
set "searchChar=\"
for /f "delims=" %%a in ('
findstr /n "^" "%inputFile%"
') do for /f "delims=:" %%b in ("%%~a") do (
set "line=%%a"
for /f %%c in ('
cmd /u /v /e /q /c"(echo(!line:*:=!)"^|find /c "%searchChar%"
') do echo Line %%b has %%c characters
)
The input file is readed using findstr /n
to get all the lines in the file with a number prefix (both for output "decoration" and to ensure all the lines in the file are processed). Each line is processed inside a pipe, from cmd
to find
. The cmd
instance is started with unicode output (/u
) so when the readed line is echoed, the output will be a two bytes sequence for each input character, one of them a 0x0
ASCII character. The find
command sees the 0
as a line terminator, so we get each character in the input line as one separated line. Now, the find
command counts in how many lines the searched character happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With