读取文件没一行,默认空格分割拆分成数组,遍历每个单词,
使其作为map的key,map的value为计数器,当获取某个key,没有值时value初始1,有值时自增1
declare -A map function test3() { local temp="" local arr="" while read line; do arr=(${line}) for ele in ${arr[@]}; do temp=${map[${ele}]} if [ -z "${temp}" ]; then map[${ele}]=1 else # map[${ele}]=$[ ${temp} + 1 ] ((map[${ele}]++)) fi done done < nowcoder.txt } function echoMap() { echo "keyList=${!map[@]}" echo "valueList=${map[@]}" } function foreach() { for ele in ${!map[@]}; do echo "${ele} ${map[${ele}]}" done } function processor() { test3 temp=$(foreach) echo "${temp}" | sort -k2 } processor