Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count an exact character in one line - cmd

Tags:

batch-file

cmd

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.

like image 841
j b Avatar asked Jan 31 '17 14:01

j b


3 Answers

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: ", %, =, *, ~.

like image 134
aschipfl Avatar answered Nov 19 '22 00:11

aschipfl


@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%
like image 4
Aacini Avatar answered Nov 18 '22 23:11

Aacini


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.

like image 3
MC ND Avatar answered Nov 18 '22 23:11

MC ND