使用 Bash 计算字符串中字符的出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16679369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count occurrences of a char in a string using Bash
提问by Jericob
I need to count the number of occurrences of a char in a stringusing Bash.
我需要使用 Bash 计算字符串中某个字符的出现次数。
In the following example, when the char is (for example) t
, it echo
sthe correct number of occurrences of t
in var
, but when the character is comma or semicolon, it prints out zero:
在下面的示例中,当字符是(例如)t
,它echo
š出现的正确的号码t
中var
,但是当字符是逗号或分号,它打印出零:
var = "text,text,text,text"
num = `expr match $var [,]`
echo "$num"
采纳答案by hek2mgl
I would use the following awk
command:
我会使用以下awk
命令:
string="text,text,text,text"
char=","
awk -F"${char}" '{print NF-1}' <<< "${string}"
I'm splitting the string by $char
and print the number of resulting fields minus 1.
我正在拆分字符串$char
并打印结果字段的数量减去 1。
If your shell does not support the <<<
operator, use echo
:
如果您的 shell 不支持该<<<
运算符,请使用echo
:
echo "${string}" | awk -F"${char}" '{print NF-1}'
回答by jm666
you can for example remove all other chars and count the whats remains, like:
例如,您可以删除所有其他字符并计算剩余的内容,例如:
var="text,text,text,text"
res="${var//[^,]}"
echo "$res"
echo "${#res}"
will print
将打印
,,,
3
or
或者
tr -dc ',' <<<"$var" | awk '{ print length; }'
or
或者
tr -dc ',' <<<"$var" | wc -c #works, but i don't like wc.. ;)
or
或者
awk -F, '{print NF-1}' <<<"$var"
or
或者
grep -o ',' <<<"$var" | grep -c .
or
或者
perl -nle 'print s/,//g' <<<"$var"
回答by Robin Hsu
You can do it by combining tr
and wc
commands. For example, to count e
in the string referee
您可以通过组合tr
和wc
命令来完成。例如,e
在字符串中 计数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.
说明:Commandtr -cd 'e'
删除除 'e' 以外的所有字符,Commandwc -c
计算剩余字符数。
Multiple lines of input are also good for this solution, like command cat mytext.txt | tr -cd 'e' | wc -c
can counts e
in the file mytext.txt
, even thought the file may contain many lines.
多行输入也适用于该解决方案,例如命令cat mytext.txt | tr -cd 'e' | wc -c
可以e
在文件中计数mytext.txt
,甚至认为文件可能包含多行。
回答by rmanna
Building on everyone's great answers and comments, this is the shortest and sweetest version:
基于每个人的精彩回答和评论,这是最短和最甜蜜的版本:
grep -o "$needle" <<< "$haystack" | wc -l
grep -o "$needle" <<< "$haystack" | wc -l
回答by user2508516
awk works well if you your server has it
如果你的服务器有 awk,它运行良好
var="text,text,text,text"
num=$(echo "${var}" | awk -F, '{print NF-1}')
echo "${num}"
回答by Freeman
also check this out, for example we wanna count t
也检查一下,例如我们想要数 t
echo "test" | awk -v RS='t' 'END{print NR-1}'
or in python
或在 python
python -c 'print "this is for test".count("t")'
or even better, we can make our script dynamic with awk
甚至更好,我们可以使我们的脚本动态 awk
echo 'test' | awk '{for (i=1 ; i<=NF ; i++) array[$i]++ } END{ for (char in array) print char,array[char]}' FS=""
in this case output is like this :
在这种情况下,输出是这样的:
e 1
s 1
t 2
回答by Mathew P V
I Would suggest the following:
我建议如下:
var="any given string"
N=${#var}
G=${var//g/}
G=${#G}
(( G = N - G ))
echo "$G"
No call to any other program
不调用任何其他程序