计算并打印在列串的平均值 [英] calculate and print the average value of strings in a column
问题描述
我得到一个.txt文件与价值观的2列。它们是二维坐标,所以第一列重新present x值,第二个是z值。不幸的是有一些线具有相同的x值,但不同的z值。我想计算以单个z到一个x关联的z值的平均值。
什么我有一个样品是:
I got a .txt file with 2 columns of values. They are 2D coordinates, so the first column represent the x value and the second one is the z value. Unfortunately there are some lines with the same x value but a different z value. I'd like to calculate the average of the z values in order to associate a single z to a single x. A sample of what i have is:
435.212 108.894
435.212 108.897
435.212 108.9
435.212 108.903
正如你所看到的x值435.212与4种不同的Z值相关联。
我想拥有的是:
As you can see the x value 435.212 is associated with 4 different z value. What i'd like to have is:
435.212 108.8985
,其中108.8985是(108.894 + 108.897 + 108.9 + 108.903)/ 4的结果。
我当然不希望修改其他X和Z值,所以结果会是这样的:
where 108.8985 is the result of (108.894+108.897+108.9+108.903)/4. Of course i don't want to modify the other x and z values, so the result would be something like that:
在
435.238 108.9
435.25 108.9
435.262 108.9
435.275 108.9
435.212 108.894 <---
435.212 108.897<---
435.212 108.9<---
435.212 108.903<---
在
435.238 108.9
435.25 108.9
435.262 108.9
435.275 108.9
435.212 108.8985 <---average
与单个x关联的z值的数目可以变化。
The number of z values associated with a single x may vary.
我使用的是Linux命令行,我虽然用awk的工作,虽然任何其他程序/实用工具,我可以在Linux命令行上使用可能是不错的。
I am using the linux command line and I though to use awk for the job, although any other program/utility i can use on a linux command line could be good.
推荐答案
这是 AWK
的一种方法:
$ awk '{a[$1]+=$2; ++b[$1]} END {for (i in a) print i, a[i]/b[i]}' file
435.212 108.899
435.25 108.9
435.238 108.9
435.262 108.9
435.275 108.9
说明
{a [$ 1] + = $ 2; ++ B〔$ 1]}
- 在阵列中的存储z值(第2列)
A
。 - 阵列中的存储每个x值(第1列)元素的含量
B
。
- Store the z values (2nd column) in the array
a
. - Store the amount of elements for each x value (1st column) in the array
b
.
END {为(我的)打印I,A [I] / B [I]}
- 打印结果通过存储在数组中的值循环。
要拥有另一号码格式(例如4浮点值),你也可以使用:
To have another number format (4 float values for example) you can also use:
printf "%d %.4f\n", i, a[i]/b[i]
这篇关于计算并打印在列串的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!