Count Occurences of a Character in a String
April 30, 2023 —
Gregg Szumowski
Sometimes I need to know how many times a specific character occurs in a string. The best tool to use for something like that is AWK. Let's say we're looking for the count of commas in a string:
string="text,text,text,text"
char=","
echo "${string}" | awk -F"${char}" '{print NF-1}'
Tags: cli,motd