Hi,

fxkl4...@protonmail.com wrote:
> why doesn't grep count 2 commas
> echo 'Kích thước máy xay cỏ, giá máy thế nào , phụ tùng máy mua ở đâu' |
> grep -c ,
> 1

Happens with much simpler text too:
  $ echo ',,,' | grep -c ','
  1

The man page explains it:

 -c, --count
    Suppress  normal output; instead print a count of matching lines
    for each input file.  With the -v,  --invert-match  option  (see
    below), count non-matching lines.  (-c is specified by POSIX.)

Note that it counts lines, not characters.

Counting characters by grep seems somewhat tricky:
  
https://www.unix.com/shell-programming-and-scripting/128033-grep-command-count-characters.html

An answer to this question
  
https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash
proposes
    echo "referee" | tr -cd 'e' | wc -c

  $ echo ',,,' | tr -cd ',' | wc -c
  3
  $ echo 'Kích thước máy xay cỏ, giá máy thế nào , phụ tùng máy mua ở đâu' | tr 
-cd ',' | wc -c
  2

(I hope i did not quote something that's against the code of conduct. :))


Have a nice day :)

Thomas

Reply via email to