arrays - bash 中的数组数组

标签 arrays bash shell

我正在尝试逐行读取包含由句点分隔的字段的输入文件。 我想将它们放入数组数组中,以便稍后循环遍历它们。输入似乎没问题,但将其“推送”到数组 (inData) 似乎不起作用。

代码如下:

Input file: 
GSDB.GOSALESDW_DIST_INVENTORY_FACT.MONTH_KEY
GSDB.GOSALESDW_DIST_INVENTORY_FACT.ORGANIZATION_KEY


infile=${1}

OIFS=$IFS
IFS=":"

cat ${infile} | while read line
do
      line=${line//\./:}
      inarray=(${line})
#      echo ${inarray[@]}
#      echo ${#inarray[@]}      
#      echo ${inarray[0]}
#      echo ${inarray[1]}
#      echo ${inarray[2]}

      inData=("${inData[@]}" "${inarray[@]}")
done 
IFS=$OIFS

echo ${#inData[@]}   

for ((i = 0; i < ${#inData[@]}; i++))
do
 echo $i
    for ((j = 0; j < ${#inData[$i][@]}; j++))
    do
       echo ${inData[$i][$j]}
    done
done

最佳答案

bash 中的字段嵌套框但是无法绕过看例子。

#!/bin/bash

# requires bash 4 or later; on macOS, /bin/bash is version 3.x,
# so need to install bash 4 or 5 using e.g. https://brew.sh

declare -a pages

pages[0]='domain.de;de;https'
pages[1]='domain.fr;fr;http'

for page in "${pages[@]}"
do
    # turn e.g. 'domain.de;de;https' into
    # array ['domain.de', 'de', 'https']
    IFS=";" read -r -a arr <<< "${page}"

    site="${arr[0]}"
    lang="${arr[1]}"
    prot="${arr[2]}"
    echo "site : ${site}"
    echo "lang : ${lang}"
    echo "prot : ${prot}"
    echo
done

关于arrays - bash 中的数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12317483/

相关文章:

php - 从mysql表中获取行到php数组

javascript - 使用标准验证不同类型的表单输入

java - 数组中的奇怪问题

linux - 从文件中删除范围内的行

linux - 制作脚本回答 svn+ssh passkey

javascript - 使用 .each() 获取 jQuery 数组中第一个索引的值

bash - 拦截Wget输出

bash - 如何判断当前终端 session 是否在 GNU screen 中?

linux - 如何将 LANG 设置为 ascii?

shell - 行 [ : missing `]' debugging help please