How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file

vi filename

[esc]:%s/string/string/g

will report how many times the substitution was performed... there has to be a better way, but off the top of my head that one works.

1 Like

If you're using vim, just add the 'n' flag to avoid the substitution:

:%s/string/&/gn

Thanks All, that worked :slight_smile:

Use the following in vi (ex mode)

:!grep -c <pattern> <filename>

:slight_smile:

Be careful, this shows number of lines the pattern is found on. If the pattern occurs more than once on a line, it would be counted only once.

1 Like