用于计算单词频率的Bash脚本

时间:2017-05-06 08:40:12

标签: bash shell

我有一个这样的文件:

Today the a the a day
Hello today

我想用bash脚本计算文件中的单词频率:

#!/bin/bash
declare -A hashmap
cat words.txt | while read line
do
    IFS=' ' read -r -a l <<< "$line"
    for s in "${l[@]}"
    do
        if [ ${hashmap[$s]+abc} ]
        then
            let hashmap[$s]=${hashmap[$s]}+1
        else
            hashmap+=([$s]=0)
        fi
    done
    echo ${#hashmap[@]}
    #print 4(the length of the associated array )
done
#Weird! It seems that hashmap(associated array) goes out scope!
echo ${#hashmap[@]}
#print 0
for k in ${!hashmap[*]}
do
        printf "%s\t%s\n" $k ${hashmap[$k]}
done

你能告诉我为什么它什么都没输出?

0 个答案:

没有答案