算上字符的出现在使用bash的字符串 [英] count occurrences of char in string using bash
本文介绍了算上字符的出现在使用bash的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用bash数字符的数字出现在串,
当使用例如t的例程的返回是出现的次数,但使用逗号或分号等时,是CERO
I need count the numbers occurrences of char in string using bash, when use for example "t" the return of routine is the number of occurrences , but when using comma or semicolon etc, is cero.
例如:
var = "text,text,text,text"
num = `expr match $var [,]`
echo "$num"
返回0。
推荐答案
您可以使用使用以下bash脚本的grep
和厕所
来算字符:
You can use the following bash script that uses grep
and wc
to count the chars:
needle=","
var="text,text,text,text"
number_of_occurrences=$(grep -o "$needle" <<< "$var" | wc -l)
这篇关于算上字符的出现在使用bash的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文