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

前端 未结 9 1493
生来不讨喜
生来不讨喜 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:13

    May be it's a little bit late, but the following script worked for me (the source file contained quote characters, this is why I used 'usebackq' parameter). The caret sign(^) acts as escape character in windows batch scripting language.

    @setlocal enableextensions enabledelayedexpansion    
    SET TOTAL=0
    FOR /F "usebackq tokens=*" %%I IN (file.txt) do (
        SET LN=%%I
        FOR %%J IN ("!LN!") do (
            FOR /F %%K IN ('ECHO %%J ^| FIND /I /C "searchPhrase"') DO (
                @SET /A TOTAL=!TOTAL!+%%K
            )
        )
    )
    ECHO Number of occurences is !TOTAL!
    

提交回复
热议问题