How to find the number of occurrences of a string

2019-02-09 07:56发布

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 this but it just prints the matching lines. (btw : all e-mails are contained in one line)

findstr /c:"@" mail.txt

9条回答
女痞
2楼-- · 2019-02-09 08:32

Very simple solution:

grep -o "@" mail.txt | grep -c .

Remember a dot at end of line!

Here is little bit more understandable way:

grep -o "@" mail.txt | grep -c "@"

First grep selects only "@" strings and put each on new line.

Second grep counts lines (or lines with @).

The grep utility can be installed from GnuWin project or from WinGrep sites. It is very small and safe text filter. The grep is one of most usefull Unix/Linux commands and I use it in both Linux and Windows daily. The Windows findstr is good, but does not have such features as grep.

Installation of the grep in Windows will be one of the best decision if you like CLI or batch scripts.

查看更多
ら.Afraid
3楼-- · 2019-02-09 08:32

This is how I do it, using an AND condition with FINDSTR (to count number of errors in a log file):

SET COUNT=0
FOR /F "tokens=4*" %%a IN ('TYPE "soapui.log" ^| FINDSTR.exe /I /R^
 /C:"Assertion" ^| FINDSTR.exe /I /R /C:"has status VALID"') DO (
  :: counts number of lines containing both "Assertion" and "has status VALID"
  SET /A COUNT+=1
)
SET /A PASSNUM=%COUNT%

NOTE: This counts "number of lines containing string match" rather than "number of total occurrences in file".

查看更多
【Aperson】
4楼-- · 2019-02-09 08:33

Use this:

type file.txt | find /i "@" /c
查看更多
登录 后发表回答