简体   繁体   English

如何将输出分配给两个bash数组变量

[英]How can I assign output to two bash arrays variables

There are vaguely similar answers here but nothing that could really answer my question. 这里大概有类似的答案,但是没有什么可以真正回答我的问题。 I am at a point in my bash script where I have to fill two arrays from an output that looks like the following: 我在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

What I need to do is pull out the files and line numbers in there own separate arrays. 我需要做的是在各自独立的数组中提取文件和行号。 So far I have: 到目前为止,我有:

read FILES LINES <<<($(echo $INPUTFILES | xargs grep -n $id | cut -f1,2 -d':' | awk '{print $1 " " $2}' with a modified IFS=':' but this doesn't work. I'm sure there are better solutions since I am NOT a scripting or tools wizard. read FILES LINES <<<($(echo $INPUTFILES | xargs grep -n $id | cut -f1,2 -d':' | awk '{print $1 " " $2}'并具有经过修改的IFS=':'但我肯定没有更好的解决方案,因为我不是脚本或工具向导。

read cannot populate two arrays at a time like it can populate two regular parameters using word-splitting. read不能一次填充两个数组 ,就像它可以使用单词拆分填充两个常规参数一样。 It's best to just iterate over the output of your pipeline to build the arrays. 最好只迭代管道的输出以构建阵列。

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

Here, I'm letting read do the splitting and discarding of the the tail end of grep 's output in place of using cut and awk . 在这里,我让read进行grep输出尾部的拆分和丢弃,以代替使用cutawk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM