How to find the number of occurrences of a string in file using windows command line?

前端 未结 9 1437
生来不讨喜
生来不讨喜 2021-01-01 23:35

I have a huge files with e-mail addresses and I would like to count how many of them are in this file. How can I do that using Windows\' command line ?

I have tried

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 00:34

    Why not simply using this (this determines the number of lines containing (at least) an @ char.):

    find /C "@" "mail.txt"
    

    Example output:

    ---------- MAIL.TXT: 96
    

    To avoid the file name in the output, change it to this:

    find /C "@" < "mail.txt"
    

    Example output:

    96
    

    To capture the resulting number and store it in a variable, use this (change %N to %%N in a batch file):

    set "NUM=0"
    for /F %N in ('find /C "@" ^< "mail.txt"') do set "NUM=%N"
    echo %NUM%
    

提交回复
热议问题