you can, for example, delete all other characters and calculate what remains, for example:
var="text,text,text,text" res="${var//[^,]}" echo "$res" echo "${#res}"
will print
,,, 3
or
tr -dc ',' <<<"$var" | awk '{ print length; }'
or
tr -dc ',' <<<"$var" | wc -c
or
awk -F, '{print NF-1}' <<<"$var"
or
grep -o ',' <<<"$var" | grep -c .
or
perl -nle 'print s/,//g' <<<"$var"
jm666 May 21 '13 at 21:19 2013-05-21 21:19
source share