【问题标题】:Iterate over three arrays simultaneously in bash在 bash 中同时迭代三个数组
【发布时间】:2019-09-21 11:10:46
【问题描述】:

我想迭代 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

【问题讨论】:

  • 你的问题对我来说不是很清楚,因为运行你的脚本会得到你想要的相同的预期输出。
  • 对不起,我的错误。我用更多细节更新了问题

标签: arrays bash loops


【解决方案1】:
sed 1d file | while IFS="," read team1 team2 team3; do echo "$team1:$team2:$team3"; done 

输出:

CPMSCF:cpmscf:管道操作员 CPOCNCF:cpocncf:管道操作员

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2015-05-26
    相关资源
    最近更新 更多