计算字符串中的char使用计数 [英] calculate count of char use in string
本文介绍了计算字符串中的char使用计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使该程序正常工作,我不能,我想要的脚本必须计算字符串中的char使用计数例子 :字符串=好结果= g1o2d1有没有什么办法可以像我的示例那样编写能够精确计算字符串数的脚本?
I'm trying to make this program work and I can't, The script i want must should calculate count of char use in string example : string=good result=g1o2d1 is there any way to write script that calculate exactly count of string like my example or not ?
#!/bin/bash
string=ssstttrrrriiiinnnnngg
z=$( for((i=0;i<${#string};i++)) do; echo ${string:i:1}; done | uniq -c )
echo $z
我的结果:
my result :
s 3 t 3 r 4 i 4 n 5 g 2
但是为了分析某些文档,我想要脚本来计算char,例如firstchar1 = $(bash脚本)......我需要该值才能使用另一个脚本请给我提意见问候
but for analysis some document i want script to calculate char some like firstchar1=$( bash script ) ...... i need that value for use another script please advise me regards
推荐答案
$ echo "abaaacdefg" | grep -o .
a
b
a
a
a
c
d
e
f
g
$ echo "abaaacdefg" | grep -o .| sort
a
a
a
a
b
c
d
e
f
g
$ echo "abaaacdefg" | grep -o .| sort | uniq -c
4 a
1 b
1 c
1 d
1 e
1 f
1 g
$ echo "abaaacdefg" | grep -o .| sort | uniq -c | awk '{printf $2$1}'
a4b1c1d1e1f1g1
请参阅 Bash:将字符串拆分为字符数组,然后使用命令行工具按排序顺序对重复项进行计数
这篇关于计算字符串中的char使用计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文