Using grep
, it is easy to count the occurrences of a string or regular
expression in a file. Using the -o
flag, it will print each individual match
to its own line, which can then just be counted with the wc
utility:
$ grep -o "match" ./count-matches-in-file.md | wc -l
If the file is binary, the -a
flag can be used.