shell dict 操作

时间:2021-05-10 04:43:04

shell 读取文件,利用dict 合并第一列 。

#!/bin/bash
result_file="a"
declare -A mydict total=`cat ${result_file}.bak| wc -l `
count= #cat ${result_file}.bak | while read campid devid needmd5
while read campid devid needmd5
do
#echo "$campid devid: $devid "
if [ -z "${mydict[$devid]}" ]; then
#echo "if $devide "
#mydict=([$devid]=$campid)
#export
mydict+=([$devid]="$campid")
echo "aaafter mydict keys ${!mydict[*]} "
else
#$mydict[$devid]+="," + $campid
mydict+=([$devid]+=","$campid)
fi ((count+=))
#文件读结束时候输出所有keys
if [ $count -eq $total ]; then
echo "count : $count "
echo "bbbfter mydict keys ${!mydict[*]} "
fi
echo "before return ${!mydict[*]} "
done < ${result_file}.bak echo "before return ${!mydict[*]} " #echo ${!mydic[*]}
echo "after mydict keys ${!mydict[*]} " echo "ccc $ccc "
for key in $(echo ${!mydict[*]})
do
echo " k: $key : ${mydict[$key]}"
done

开始读时候用 cat ${result_file}.bak | while read campid devid needmd5 的形式,最后

mydict的keys 一直为空,最后薛大佬帮忙修改为从文件中读 不用管道的形式解决。

a.bak 文件内容

188302021 b20892db-485d-4ae9-af77-f75791af824a 0
183935026 DC3FC748-EAFD-4D4A-8F64-B9F3517EF549 0
183935026 9B9E985A-3F1F-4D76-A15E-E5B79F5E85FD 0
183935026 75E9144A-2A42-4F32-973D-BF77DF5A25AF 0