简体   繁体   English

在 Bash 中将循环中的多个输出组合成一个带有逗号的文本文件

[英]Combining multiple outputs from a loop into one text file with commas in Bash

I am testing out a method of motion detection using bash script.我正在测试一种使用 bash 脚本的运动检测方法。 I am running an ImageMagick compare command and outputting the result into a text file.我正在运行 ImageMagick 比较命令并将结果输出到文本文件中。

A loop creates one output every time it runs through.循环每次运行时都会创建一个输出。 I want each output to be placed into a single text file and to be separated by commas.我希望将每个输出放入单个文本文件并用逗号分隔。

The code I am using is currently:我目前使用的代码是:

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

This code does proceed to put all of the data into one text file, but the data is displayed together, and just looks like one big number.这段代码确实继续将所有数据放入一个文本文件中,但数据显示在一起,看起来就像一个大数字。

At the moment the data is put into the text file, however it is displayed like: "4873343460936622743393154537"目前数据被放入文本文件,但显示如下:“4873343460936622743393154537”

When I want it to read: "4873,343,4609,366,2274,339,315,4537"当我希望它阅读时:“4873,343,4609,366,2274,339,315,4537”

You could do this:你可以这样做:

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 adds a comma between each output of compare in the loop. printf在循环中compare的每个输出之间添加一个逗号。 The condition prevents a comma being added on the last iteration.该条件防止在最后一次迭代中添加逗号。

Slightly different :稍微不一样 :

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.

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