192. Word Frequency
For example, assume that words.txt
has the following content:
the day is sunny the the the sunny is isYour script should output the following, sorted by descending frequency:
the 4 is 3 sunny 2 day 1
1、题意应该是按空格,默认空格,或者-F“ ”
awk -F" " '{for(i=1;i<=NF;i++){array[$i]+=1;}} END{for(s in array){print s" "array[s];}}' words.txt|sort -nr -k 2
2、如果是句子,有标点符号,那么需要sed替换为空格sed ‘s/[,.:;/!?]/ /g’ file再awk处理
学习awk命令链接:http://man.linuxde.net/awk#模式(#哼,好锚)