2015-10-05 133 views
0

这里有几乎相似的答案,但没有什么能真正回答我的问题。我在我的bash脚本一个地步,我必须从那个看起来像一个输出填补两个数组如下:如何将输出分配给两个bash数组变量

part-of-the-file1:line_32 
part-of-the-file1:line_97 
part-of-the-file2:line_88 
part-of-the-file2:line_93 

我需要做的是拉出来的文件和行号有自己单独的阵列。到目前为止,我有:

read FILES LINES <<<($(echo $INPUTFILES | xargs grep -n $id | cut -f1,2 -d':' | awk '{print $1 " " $2}' 与修改IFS=':'但这是行不通的。我确定有更好的解决方案,因为我不是一个脚本或工具向导。

回答

5

read无法填充两个阵列在某个时候,它可以使用文字分割来填充两个常规参数。最好只是迭代管道的输出来构建数组。

while IFS=: read fname lineno therest; do 
    files+=("$fname") 
    lines+=("$lineno") 
done < <(echo $INPUTFILES | xargs grep -n "$id") 

在这里,我让readgrep的输出末端的分裂,并丢弃在适当位置使用cutawk的。

+0

That worked great – jiveturkey

+0

@jnbbender,if this then answer,[accept it](http://stackoverflow.com/help/someone-answers) –