shell - Count occurrences of a char in a string using Bash -


i need count number of occurrences of char in string using bash.

in following example, when char (for example) t, echos correct number of occurrences of t in var, when character comma or semicolon, prints out zero:

var = "text,text,text,text"  num = `expr match $var [,]` echo "$num" 

you can use following bash script uses gnu grep , wc count chars:

needle="," var="text,text,text,text"  number_of_occurrences=$(grep -o "$needle" <<< "$var" | wc -l) 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -