【发布时间】:2015-10-22 00:44:44
【问题描述】:
有一个文件看起来像这样:
f1.1 f2.1 f3.1 f4.1
f1.2 f2.2 f3.2 f4.2
f1.3 f2.3 f3.3 f4.3
然后我使用第 1 列作为关联数组中的键:
declare -A arr=(); while read -r a b; do arr["$a"]="$b"; done <file
打印键和值的结果:
for K in "${!arr[@]}"; do
echo $K --- ${arr[$K]}
done
f1.1 --- f2.1 f3.1 f4.1
f1.2 --- f2.2 f3.2 f4.2
f1.3 --- f2.3 f3.3 f4.3
如果我只想将第 2 列和第 3 列存储为值怎么办?这样上面的命令给出:
f1.1 --- f2.1 f3.1
f1.2 --- f2.2 f3.2
f1.3 --- f2.3 f3.3
【问题讨论】:
标签: bash shell associative-array