写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。
为了简单起见,你可以假设:
words.txt只包括小写字母和 ' ' 。示例:
假设 words.txt 内容如下:
the day is sunny the the
the sunny is is你的脚本应当输出(以词频降序排列):
the 4
is 3
sunny 2
day 1说明:
cat 读取,tr排重,sort排序以支持uniq,uniq统计,sort逆序,awk显示
cat words.txt | tr -s '[:blank:]' '\n' | sort | uniq -c | sort -r | awk '{print$2,$1}'