[英]Combining multiple outputs from a loop into one text file with commas in Bash
我正在测试一种使用 bash 脚本的运动检测方法。 我正在运行 ImageMagick 比较命令并将结果输出到文本文件中。
循环每次运行时都会创建一个输出。 我希望将每个输出放入单个文本文件并用逗号分隔。
我目前使用的代码是:
for (( x=1; x<=$vidLength; x++))
do
#Compare current frame with previous, saving result as "difference-current"png
compare -metric RMSE -fuzz 5% previous-001.png current-001.png difference+%x+.png 2>> motionData.txt
Done
这段代码确实继续将所有数据放入一个文本文件中,但数据显示在一起,看起来就像一个大数字。
目前数据被放入文本文件,但显示如下:“4873343460936622743393154537”
当我希望它阅读时:“4873,343,4609,366,2274,339,315,4537”
你可以这样做:
for (( x=1; x<=vidLength; x++)) # no need for $ here
do
#Compare current frame with previous, saving result as "difference-current"png
compare -metric RMSE -fuzz 5% previous-001.png current-001.png difference+%x+.png 2>> motionData.txt
if (( x<vidLength ))
then
printf , >> motionData.txt
fi
done
printf
在循环中compare
的每个输出之间添加一个逗号。 该条件防止在最后一次迭代中添加逗号。
稍微不一样 :
for (( x=1; x<=$vidLength; x++))
do
#Compare current frame with previous, saving result as "difference- current"png
compare -metric RMSE -fuzz 5% previous-001.png current-001.png difference+%x+.png
echo ","
done > motionData.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.