49 lines
1.1 KiB
Bash
49 lines
1.1 KiB
Bash
# 192.统计词频
|
|
#
|
|
# [原题链接](https://leetcode-cn.com/problems/word-frequency/)
|
|
#
|
|
# 2021-07-16 09:23:44
|
|
#
|
|
# 写一个 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
|
|
#
|
|
#
|
|
# 说明:
|
|
#
|
|
#
|
|
# 不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。
|
|
# 你可以使用一行 Unix pipes 实现吗?
|
|
#
|
|
# Related Topics Shell
|
|
#
|
|
# 👍 149 👎 0
|
|
|
|
# 0ms
|
|
cat words.txt | awk -F '[ ]' '{for(i=1;i<=NF;i++){if($i!=""){a[$i]++}}}END{for(i in a){print i,a[i]| "sort -nr -k2"}}'
|
|
|
|
# 4ms
|
|
tr -s ' ' '\n' <words.txt | sort | uniq -c |sort -r| awk '{print $2 " " $1}'
|
|
cat words.txt |tr -cs "[a-z][A-Z]" "\n" |tr A-Z a-z |sort|uniq -c|sort -r|awk '{print $2,$1}'
|