我有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)
提前致谢
亚历
答案 0 :(得分: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