05 August 2008
写个bash统计文件中每个单词的频率
```shell script cat words.txt | tr -s ‘ ‘ ‘\n’ | sort | uniq -c | sort -r | awk ‘{ print $2, $1 }’
cat words.txt | xargs printf “%s\n” | sort | uniq -c | sort -nr | awk ‘{print $2,$1}’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
```shell script
declare -A arr #associative array
while IFS= read -r line
do
for word in $line
do
let arr[$word]=${arr[$word]}+1
done
done < words.txt
for key in ${!arr[@]}
do
echo $key ${arr[$key]}
done | sort -rn -k2