![基于不连续的行对列进行平均](https://linux22.com/image/154555/%E5%9F%BA%E4%BA%8E%E4%B8%8D%E8%BF%9E%E7%BB%AD%E7%9A%84%E8%A1%8C%E5%AF%B9%E5%88%97%E8%BF%9B%E8%A1%8C%E5%B9%B3%E5%9D%87.png)
我有几个这样的文件:
38
CVBR1 14.8804 -24.6979
CVBV1 16.1739 -22.9142
CVBV2 16.1376 -22.8745
CVBV4 16.0818 -22.8148
CVBV5 16.0028 -22.7777
CVBV6 16.1640 -22.7128
CVFG1 14.8981 -24.4886
CVFG2 15.0135 -24.4262
CVFG3 15.0450 -24.3434
CVFG4 14.8876 -24.2931
CVFG5 14.8898 -24.4072
CVMA1 15.2288 -23.2110
CVMA2 15.1347 -23.1612
CVMA3 15.1441 -23.1216
CVMA4 15.2699 -23.1733
CVSA1 17.1328 -25.0427
CVSA2 17.2023 -25.0928
CVSA3 17.1647 -25.0973
CVSA4 17.1062 -25.2434
CVSA5 17.0208 -25.1670
CVSA6 16.9569 -25.3100
CVSL1 16.6008 -22.8977
CVSL2 16.7091 -22.9402
CVSL3 16.7633 -22.8944
CVSL4 16.7578 -22.9826
CVSN1 16.6497 -24.3205
CVSN2 16.6219 -24.3465
CVSN3 16.6199 -24.2929
CVSN4 16.6135 -24.1318
CVST1 15.0150 -23.5251
CVST2 15.0525 -23.6041
CVST3 15.0983 -23.6659
CVST4 15.2299 -23.7426
CVST5 15.1186 -23.5200
CVSV1 16.8864 -24.9228
CVSV2 16.8748 -25.0198
CVSV3 16.8518 -25.0361
CVSV4 16.8464 -24.8741
CVMA3 CVSA6
16.9569 -25.3100
3.6722
.03092
CVMA3 CVSV3
16.8518 -25.0361
3.4578
.04678
CVSA6 CVST3
15.0983 -23.6659
2.7588
.02963
每个文件中的前 39 行完全相同。我想计算 3.6722+3.4578+2.7588+(尽可能多)/尽可能多的平均值
我做了以下事情:
for file in in_tomo_*
do
npaths=$((($nlinhas - 39) / 4))
echo $npaths
average=$(echo | awk 'BEGIN{SUM=0}{{if NR>=39 && (NR+3)%4==0} SUM+=$1}END{print SUM/'$npaths'}' $file)
echo $average
done
答案1
awk 'FNR == 1 { skip = $1 } FNR > skip && FNR%4 == 2 { sum += $1; ++n } END { print sum/n }' file
这将确定要跳过多少行,直到我们从第一行获得有趣的数据。然后在每组四行的第三行找到有趣的数据。该数据被添加sum
并n
加一。最后,n
输出总和除以,得出我们拾取的所有值的平均值。
对于给定的示例,这将输出3.29627
.