★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bhjmwdak-md.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Write a bash script to calculate the frequency of each word in a text file words.txt
.git
For simplicity sake, you may assume:github
words.txt
contains only lowercase characters and space ' '
characters.Example:bash
Assume that words.txt
has the following content:微信
the day is sunny the the the sunny is is
Your script should output the following, sorted by descending frequency:spa
the 4 is 3 sunny 2 day 1
Note:code
写一个 bash 脚本以统计一个文本文件 words.txt
中每一个单词出现的频率。htm
为了简单起见,你能够假设:blog
words.txt
只包括小写字母和 ' '
。示例:排序
假设 words.txt
内容以下:
the day is sunny the the the sunny is is
你的脚本应当输出(以词频降序排列):
the 4 is 3 sunny 2 day 1
说明:
4ms
1 # Read from the file words.txt and output the word frequency list to stdout. 2 cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'
8ms
1 # Read from the file words.txt and output the word frequency list to stdout. 2 awk '{ 3 for (i = 1; i <= NF; ++i) ++s[$i]; 4 } END { 5 for (i in s) print i, s[i]; 6 }' words.txt | sort -nr -k 2
16ms
1 # Read from the file words.txt and output the word frequency list to stdout. 2 3 # try 1 4 sed 's/ \{1,\}/\n/g' words.txt | sed '/^$/d' | sort | uniq -c | sort -nr | awk '{print $2,$1}'