leetcode 词频统计

写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。

为了简单起见,你可以假设:

  • words.txt只包括小写字母和 ' ' 。
  • 每个单词只由小写字母组成。
  • 单词间由一个或多个空格字符分隔。

示例:

假设 words.txt 内容如下:

the day is sunny the the
the sunny is is

思路:

1. tr 把空格全部转换成换行

2. sed把空行全部过滤掉

3. sort排序

4. uniq统计词频

5. sort 降序

6. awk 格式输出

cat words.txt |  tr " " "\n" | sed -e '/^$/d' | sort | uniq -c | sort -rn | awk '{print $2,$1}'

猜你喜欢

转载自blog.csdn.net/niu91/article/details/80962619