Bash:将多行输入拆分为数组

Bash:将多行输入拆分为数组

我有一个文件,其中包含多行字符串和 base64 编码数据,各行之间用逗号分隔。

例子:

1,meV9ivU4PqEKNpo5Q2u2U0h9owUn4Y8CF83TTjUNWTRQs7dEgVxnsMgf4lvg9kvxcIaM3yB4Ssim
z46M/C7YlovNUmrjOByhV1SCb/bGyv1yL7SYFnw1GHbYjdH0b6UZ7nQzJHU6VmwMo0V77vFNy6nx
rmJZ4KqW9EcjdV1plQmsVXSiZVi61+fNOHCMDmVtJ4q097geWxf4bT0/k/yRyRwi5Zr8BC64htVS
AdwOSo4PIk7xDLOzLywAYOCDQvD/zuErf1L0e8nHGz2LKdApHdEWB7Y2yM3iZyXuQ4sMx0+oX66+
FxwUulvHj+EpXtLJx5rmV7AUjr/GsNw/1aYAGPCfz0S+//Ic5pXX5rY1fZ96oFGw4a9vRiAmxe/w
ZOza6LtwuF+WUHjbIeWTUKKQGgFIM81dwVHHY7xdRnQhK5J0Zf3Xz0GzzZj5/2YFbI8q7lVkJ3ZQ
7Oqt0qdfk3aj+BQhOxmn1F55yACPBZoPUw6K8ExTHHGVGdCEiIDTu5qKHcUwK0hGAZA9Mun5KTO0
gPs9JxF8FJjkQBF7rEa6TP3pH5OwdkATH2uf+Zcmp1t6NbBymXVlsLzWZookVsaT1DNXf1I1H8Xz
8dnfh6Yl63jSr2PAhDrcOqJNM8Z9/XhBGxtlD1ela3nq6N1ErR1Gv1YZKNeNcL7O2Z3Vl2oyyDw=,U2FsdGVkX1/c8rTTO41zVT7gB+KL+n7KoNCgM3vfchOyuvBngdXDGjXTvXTK0jz6

现在,我想将内容拆分为一个数组,以便每个多行字符串都是一个数组元素。

我尝试使用 IFS,但只读取第一行:

filecontent=$(cat myfile)
IFS=',' read -a myarray <<< "$filecontent"

结果:

$myarray[0] = 1 
$myarray[1] = meV9ivU4PqEKNpo5Q2u2U0h9owUn4Y8CF83TTjUNWTRQs7dEgVxnsMgf4lvg9kvxcIaM3yB4Ssim

预期的:

$myarray[0] = 1
$myarray[1] = meV9ivU4PqEKNpo5Q2u2U0h9owUn4Y8CF83TTjUNWTRQs7dEgVxnsMgf4lvg9kvxcIaM3yB4Ssim
z46M/C7YlovNUmrjOByhV1SCb/bGyv1yL7SYFnw1GHbYjdH0b6UZ7nQzJHU6VmwMo0V77vFNy6nx
rmJZ4KqW9EcjdV1plQmsVXSiZVi61+fNOHCMDmVtJ4q097geWxf4bT0/k/yRyRwi5Zr8BC64htVS
AdwOSo4PIk7xDLOzLywAYOCDQvD/zuErf1L0e8nHGz2LKdApHdEWB7Y2yM3iZyXuQ4sMx0+oX66+
FxwUulvHj+EpXtLJx5rmV7AUjr/GsNw/1aYAGPCfz0S+//Ic5pXX5rY1fZ96oFGw4a9vRiAmxe/w
ZOza6LtwuF+WUHjbIeWTUKKQGgFIM81dwVHHY7xdRnQhK5J0Zf3Xz0GzzZj5/2YFbI8q7lVkJ3ZQ
7Oqt0qdfk3aj+BQhOxmn1F55yACPBZoPUw6K8ExTHHGVGdCEiIDTu5qKHcUwK0hGAZA9Mun5KTO0
gPs9JxF8FJjkQBF7rEa6TP3pH5OwdkATH2uf+Zcmp1t6NbBymXVlsLzWZookVsaT1DNXf1I1H8Xz
8dnfh6Yl63jSr2PAhDrcOqJNM8Z9/XhBGxtlD1ela3nq6N1ErR1Gv1YZKNeNcL7O2Z3Vl2oyyDw=
$myarray[2] = U2FsdGVkX1/c8rTTO41zVT7gB+KL+n7KoNCgM3vfchOyuvBngdXDGjXTvXTK0jz6

有人可以帮我吗?

答案1

您的尝试接近实际的解决方案。相关flag可以在read帮助中找到:

$ help read
...
      -d delim  continue until the first character of DELIM is read, rather
            than newline

它没有明确提及它,但您可以将分隔符设置为空,因此它将读取到 EOF。

$ IFS=, read -d '' -a myarray < myfile

$ echo ${myarray[2]}
U2FsdGVkX1/c8rTTO41zVT7gB+KL+n7KoNCgM3vfchOyuvBngdXDGjXTvXTK0jz6

答案2

你有没有尝试过换IFSIFS=$'\n' read -a myarray <<< "$filecontent"

我建议的原因$'\n'是因为 bash 不解释字符串文字中的转义序列。

答案3

我假设在您的预期结果中, $myarray[2] 的第一个实例应该是 $myarray[1]。

您可以通过在将文件内容分配给 shell 变量之前删除嵌入的换行符来实现所需的结果:

filecontent=$(tr -d \\n <myfile)

答案4

用于-d ''读取整个文件而不是仅读取一行。

 -d delim   continue until the first character of DELIM is read, rather
            than newline
$ cat file.txt 
abcd
efgh
$ IFS=$'\n' read -a ARR < file.txt
$ echo ${#ARR[@]}
1 # only 1 element
$ IFS=$'\n' read -a ARR -d '' < file.txt
$ echo ${#ARR[@]}
2 # whole file

另一件事:如果您使用 ,
请确保您的脚本中没有,如(最后)所述:set -e-d ''read --help

 Exit Status:   
    The return code is zero, **unless end-of-file is encountered**...
$ IFS=$'\n' read -a ARR < file.txt
$ echo $?
0
$ IFS=$'\n' read -a ARR -d '' < file.txt
$ echo $?
1 # Your script will exit if you `set -e`

相关内容