【发布时间】:2016-02-02 20:54:56
【问题描述】:
我编写了一个代码来计算 zscore,它计算一个文件的平均值和标准差,并使用另一个文件中的行中的一些值,如下所示:
mean=$(awk '{total += $2; count++} END {print total/count}' ABC_avg.txt)
#calculating mean of the second column of the file
std=$(awk '{x[NR]=$2; s+=$2; n++} END{a=s/n; for (i in x){ss += (x[i]-a)^2} sd = sqrt(ss/n); print sd}' ABC_avg.txt)
#calculating standard deviation from the second column of the same file
awk '{if (std) print $2-$mean/$std}' ABC_splicedavg.txt" > ABC.tmp
#calculate the zscore for each row and store it in a temporary file
zscore=$(awk '{total += $0; count++} END {if (count) print total/count}' ABC.tmp)
#calculate an average of all the zscores in the rows and store it in a variable
echo $motif" "$zscore
rm ABC.tmp
但是,当我执行此代码时,在创建临时文件的步骤中,我收到一个错误 致命:除零尝试,实现此代码的正确方法是什么? TIA 我使用了 bc -l 选项,但它给出了一个非常长的浮点整数版本。
【问题讨论】:
-
bash 变量在
awk单引号中不可见。您必须使用-v选项定义它们。但是,更好的是重新设计,以便在同一个脚本中计算所有内容,这样您就不需要在脚本之间传递变量。
标签: awk floating-point division