在bash中同时迭代三个数组

时间:2019-09-21 11:10:46

标签: arrays bash loops

我要遍历3个数组,应该得到如下输出,有人可以帮忙吗?

~ cat new.csv
team1,team2,team3
CPMSCF,cpmscf,pipelineoperator
CPOCNCF,cpocncf,pipelineoperator

team1="$(cat new.csv | cut -d, -f1 | tail -n +2)"
team2=$(cat new.csv | cut -d, -f2 | tail -n +2)
team3=$(cat new.csv | cut -d, -f3 | tail -n +2)

for index in ${!team1[*]}; do
    echo "${team1[$index]}:${team2[$index]}:${team3[$index]}"
done

输出我得到的是:

CPMSCF
CPOCNCF:cpmscf
cpocncf:pipelineoperator
pipelineoperator

但应该是

CPMSCF:cpmscf:pipelineoperator
CPOCNCF:cpocncf:pipelineoperator

1 个答案:

答案 0 :(得分:0)

sed 1d file | while IFS="," read team1 team2 team3; do echo "$team1:$team2:$team3"; done 

输出:

CPMSCF:cpmscf:pipelineoperator
CPOCNCF:cpocncf:pipelineoperator