'Linux - How to count occurrences of character 'i' in a word (not a file)

How can I print the number of occurrences of "i" in Pneumonoultramicroscopicsilicovolcanoconiosis?

How can I approach this with the commands grep and wc?



Solution 1:[1]

echo "Pneumonoultramicroscopicsilicovolcanoconiosis" | grep -o i | wc -l

Solution 2:[2]

This should do it:

echo "Pneumonoultramicroscopicsilicovolcanoconiosis"|tr -cd 'i'| wc -c

Solution 3:[3]

$ echo "Pneumonoultramicroscopicsilicovolcanoconiosis" | awk '{print gsub(/i/,"")}'
6

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gjermund Jensvoll
Solution 3 fpmurphy