Skip to content

Instantly share code, notes, and snippets.

@herohenu
Created March 7, 2018 03:46
Show Gist options
  • Save herohenu/c02c366e6eba721801bcf4d170b93c85 to your computer and use it in GitHub Desktop.
Save herohenu/c02c366e6eba721801bcf4d170b93c85 to your computer and use it in GitHub Desktop.
shell  dict 操作,如果有数据则聚合,没有则追加入dict
declare -A mydict
count=0
errstr="000000000000"
while read campid devid needmd5
do
#计数器获取当前遍历的是多少行
((count+=1))
if [[ $devid =~ $errstr ]] ;then
continue
fi
#dict 把devid当作key,campid当作value
if [ -z "${mydict[$devid]}" ]; then
mydict+=([$devid]="$campid")
else
mydict+=([$devid]+=","$campid)
fi
if [ $needmd5 -eq 1 ]; then
md5devid="`echo -n $devid|md5sum |awk '{print $1}'|tr '[a-z]' '[A-Z]'`"
echo -e "${ md5devid}\t${campid}" >>${result_file}
fi
#文件行遍历完成的时候才会
if [ $count -eq $total ]; then
for key in $(echo ${!mydict[*]}); do
echo -e "$key\t${mydict[$key]}" >> ${result_file}
done
fi
done < ${result_file}.bak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment