Get vim to display the number of commas in one line

I have a long string with many commas. I would like to know the number of commas per line. How can I do this with vim?

Also, I want to do this for non-comma characters.

Specifying the line number and character should give me the number of occurrences of this character.

eg:

:charmap/50/, 

should give me the number of commas on line 50.

+6
source share
3 answers

Just search and count matches:

 :s/,//gn 
+13
source
 :s/,//gn 

reports the number in the current line without making a substitution

 :50s/,//gn 

does the same for line 50.

See :h s_flags for /n .

+5
source

I do not know about VIM, but I get the desired results, as shown below.

 # cat t.txt ,,,, ,, ,, ,, ,, , ,,,, ,, ,, , ,,,,, 

and that's how I think not. commas in each line:

 # sed 's/[^,]//g' t.txt | awk '{ print length }' 10 7 2 2 1 0 5 
0
source

Source: https://habr.com/ru/post/945828/


All Articles