1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
The months of learning in Old Boy education are the few months that I think the time efficient is the most.I had also studied at other training institutions before, but I was hard to understand what the tutor said and hard to follow. It was just too much to learn with no outline.
来自中企动力面试题
为了能便代码可读性更强我将字符串放入一个变量:Words
按单词出现的频率进行排序
分析一下,这道题首先要想办法去掉特殊字符,简单的说只保留字母和空格以便于我们处理
echo $Words|grep -Eo '[a-Z]+'
这里我们只过滤了所有的大小写字母,并且是连接出现的,由于我们加-o
参数,因此显示的是每一次匹配的字符
有了这个数据就好办了,我们可以进行排序、去重、统计、再排序。就达到我们想要的效果了。
echo $Words|grep -Eo '[a-Z]+'|sort|uniq -c|sort -nr
按字母出现频率降序排序
分析:按字母排序就要在原来的基础上去掉中间的所有空格,然后用grep一个字符一个字符去匹配,然后排序,去重,统计,再排序,就完成了。
echo $Words|sed -r 's#[\., ]##g'
这样就只剩下字母了,有了这个数据就很简单了往下看
echo $Words|sed -r 's#[\., ]##g'|grep -o '.'|sort|uniq -c|sort -nr