发新话题
打印

找出某个字符在字符串中出现的次数

找出某个字符在字符串中出现的次数

You can do it by combining tr and wc commands. For example, to count e in the string referee

echo "referee" | tr -cd 'e' | wc -c
output

4
Explanations: Command tr -cd 'e' removes all characters other than 'e', and Command wc -c counts the remaining characters.

参考:
https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash

TOP

发新话题