在一行中算出一个确切的字符-cmd [英] count an exact character in one line - cmd
本文介绍了在一行中算出一个确切的字符-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.
例如,字符串"aa\bb\cc\dd\"
中\
的计数为4.
For example, the count of \
in the string "aa\bb\cc\dd\"
would be 4.
find
和findstr
仅显示包含确切字符的行数.
The find
and the findstr
show only the number of lines which is contains the exact character.
推荐答案
@echo off
setlocal
set "string=aa\bb\cc\dd\"
set "count=-1"
for %%a in ("%string:\=" "%") do set /A count+=1
echo %count%
只要字符串不包含通配符,此方法就可以正常工作:*?
;如果需要的话,我将使用相同的npocmaka的方法,但是以一种更简单的方式编写:
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%
这篇关于在一行中算出一个确切的字符-cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文