如何拆分输出并将其存储在数组中?

Mah*_*ari 10 bash

这是输出:

3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, h.264, 1029.1, 31, 31, 0,0,0.000000,31,31,0,0,0.000000,7,0,0,0.000000,30,1280 720,10,0,0,0.000000,30,1280 720
Run Code Online (Sandbox Code Playgroud)

我尝试了 2 个场景:

  1. 存储在数组中

      @arr=split(',',$stats);
      echo "statistics: $stats"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 存储在变量中

     echo $stats | cut -d ',' -f | read s1
     echo $s1
    
    Run Code Online (Sandbox Code Playgroud)

但是这两种情况都不起作用。

Mat*_*Mat 10

对于单行输入,您可以使用这样的东西:

IFS="," read -ra arr <<< "$foo"
Run Code Online (Sandbox Code Playgroud)

演示:

$ cat t.sh 
foo="3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, ..." # snipped

IFS="," read -ra arr <<< "$foo"
echo ${#arr[@]}
echo ${arr[0]}
echo ${arr[30]}
$ ./t.sh 
31
3
1280 720
Run Code Online (Sandbox Code Playgroud)

积分:基于 bash 中的分隔符拆分字符串?Johannes Schaub 的回答。检查那里的其他答案。