Board logo

标题: 找出某个字符在字符串中出现的次数 [打印本页]

作者: linda    时间: 2023-6-2 09:38     标题: 找出某个字符在字符串中出现的次数

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




欢迎光临 中神通公司交流论坛 (http://www.trustcomputing.com.cn/bbs/) Powered by Discuz! 6.0.0