在Bash Shell脚本中创建.csv

时间:2014-12-05 22:36:42

标签: bash shell

我正在尝试在Bash shell脚本中生成.csv文件。请考虑以下代码:

#!/bin/bash

for f1 in *_8_kHz.wav
do
   for f2 in *_8_kHz.wav
   do
      # echo "pesq +8000 $f1 $f2"
      echo -n `pesq +8000 $f1 $f2 | grep Prediction | rev | cut -b1-5 | rev`
   done

   echo
done

当然,除了每行以逗号结尾之外,这是有效的。以下是示例输出:

4.500,1.029,1.651,1.475,1.698,1.706,
1.550,4.500,1.477,1.148,1.788,1.478,
1.251,0.958,4.500,1.472,2.091,1.800,
0.961,1.154,1.550,4.500,1.702,1.501,
1.194,0.974,1.356,1.206,4.500,1.626,
0.857,0.960,1.091,1.064,2.012,4.500,

省略这些尾随逗号的最有效方法是什么?

2 个答案:

答案 0 :(得分:0)

以下是否足够有效?

echo -n `pesq +8000 $f1 $f2 | grep Prediction | rev | cut -b1-5 | rev` | sed 's/,$//'

正在使用sed删除行,行之前的最后$

答案 1 :(得分:0)

来自https://stackoverflow.com/a/18262725/2796606

foo=`pesq +8000 $f1 $f2 | grep Prediction | rev | cut -b1-5 | rev`

echo -n ${foo%?}