一行shell实现统计单词词频

写一个 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 words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rnk1 | awk '{print $2,$1}'

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-frequency
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

发布了230 篇原创文章 · 获赞 160 · 访问量 82万+

猜你喜欢

转载自blog.csdn.net/happyAnger6/article/details/104366726