shell之計算文本中單詞出現頻率

Word Frequency(https://leetcode.com/problems/word-frequency/description/)code

Example:ip

Assume that words.txt has the following content:leetcode

the day is sunny the the
the sunny is is

Your script should output the following, sorted by descending frequency:io

the 4
is 3
sunny 2
day 1

而後來看看強大的 awkawk

awk -vRS='( |\n)+' '{a[$0]++}END{for(i in a)m[a[i]]=i;i=asort(a);for(;i;)print m[a[i]],a[i--]}' words.txt
 sort

相關文章
相關標籤/搜索