2016-10-26 669 views
0

我正在尝试创建一个脚本,该脚本可以计算多个行的平均值计算多个列的平均值

这个数字将取决于我拥有的样本数量,因样本数量而异。

这些文件的一个例子是在这里:

24 1 2.505 
24 2 0.728 
24 3 0.681 
48 1 2.856 
48 2 2.839 
48 3 2.942 
96 1 13.040 
96 2 12.922 
96 3 13.130 
192 1 50.629 
192 2 51.506 
192 3 51.016 

平均值在第3列和,计算

第二列表示样品的3在该特定的数量,案件。

因此,我应该在这里获得4值

每3行一个平均值。

我已经试过类似:

count=3; 
total=0; 

for i in $(awk '{ print $3; }' ${file}) 
do 
    for j in 1 2 3 
    do 
    total=$(echo $total+$i | bc) 
    done 
    echo "scale=2; $total/$count" | bc 
done 

但它不给我正确的答案,而不是我想它计算每组的三排的平均值。

平均值被计算在第3列和

第二列表示样品,3在该特定情况下的数量。

因此,我应该在这里获得4值

每3行一个平均值。

我已经试过类似:

count=3; 
total=0; 

for i in $(awk '{ print $3; }' ${file}) 
do 
    for j in 1 2 3 
    do 
    total=$(echo $total+$i | bc) 
    done 
    echo "scale=2; $total/$count" | bc 
done 

但它不给我正确的答案,而不是我想它计算每组的三排的平均值。

预计输出

24 1.3046  
48 2.879  
96 13.0306  
192 51.0503  
+1

让我看看我是否理解你。你想要三列的第三列的平均值?预期产出是多少? – VM17

+1

我想玩猜谜游戏! –

回答

1

显然我对这个问题提出了第三种观点。在awk中:

$ awk 'NR>1 && $1!=p{print p, s/c; c=s=0} {s+=$3;c++;p=$1} END {print p, s/c}' file 
24 1.30467 
48 2.879 
96 13.0307 
192 51.0503 
1

您可以使用以下awk脚本:

awk '{t[$2]+=$3;n[$2]++}END{for(i in t){print i,t[i]/n[i]}}' file 

输出:

1 17.2575 
2 16.9988 
3 16.9423 

这是更好,因为在它的意见多行脚本解释说:

# On every line of input 
{ 
    # sum up the value of the 3rd column in an array t 
    # which is is indexed by the 2nd column 
    t[$2]+=$3 
    # Increment the number of lines having the same value of 
    # the 2nd column 
    n[$2]++ 
} 
# At the end of input 
END { 
    # Iterate through the array t 
    for(i in t){ 
     # Print the number of samples along with the average 
     print i,t[i]/n[i] 
    } 
} 
+0

我们对这个问题显然有不同的理解。 – VM17

+0

我们希望OP能提供预期的输出。 – hek2mgl

+0

@ hek2mgl很好解释,谢谢! – Manolete