1

我确实有xml,其中确实有类似的行

#<pdfrwt "2"> n_vars code(1) ... code1(2).... code1(n_vars) code2(1) ... code2(n_vars) code3(1) ... code3(n_vars)</pdfrwt>

所以实际上,每次根据“n_vars”,我确实有一个对应于三个类的数字序列,即code1、code2、code3,对于每个类,我得到每个类的“n_vars”条目。所以,我怎样才能巧妙地阅读 bash 中的行(好的,我知道 ;-) 并分配一个“n_vars”多维变量,例如

output1[1]=code1(1)
output1[2]=code1(2)
...
output1[n]=code1(n)
and likewise

output2[1]=code2(1)
..
output2[1]=code2(1)

提前致谢

亚历克斯

4

1 回答 1

1

您可以将整行读入一个数组,然后取出数组的切片。

# hdr gets the first field, n_vars gets the second, and rest gets
# the remaining values
read hdr n_vars rest < file

# Split rest into a proper array
array=( $rest )
# Copy the desired slices from array into the three output arrays.
output1=( "${array[@]:0:n_vars}")
output2=( "${array[@]:n_vars:n_vars}" )
output3=( "${array[@]:2*n_vars:n_vars}" )

如何访问值的一些示例:

echo ${output1[0]}    # The first element of output1
echo ${output2[3]}    # The fourth element of output2
echo ${output3[$#output3]}  # The last element of output3
echo "${output1[@]}"   # All the elements of output1
于 2012-12-12T20:11:55.337 回答