闲的无聊写个脚本处理数据 O(∩_∩)O~
输入是这样的:
1,10,21
2,12,19
3,9,25
…
第一列是编号从1到n,第二列是特性值,第三列是属性值。
要做的是:统计属性值的平均数,仅当特性值大于某个阈值;简单起见,这个阈值取特性值的中位数。
思路:先计算行数,再找出特性值的中位数,然后计算平均值。
#!/bin/bash
# csv file name
file=$1
# get line number
line=$(wc -l $file | grep -Po '\d+')
# get median number among $2
th=`awk -F ',' '{print $2}' $file | sort -nr | sed -n $(($line/2+1))'q;'$(($line/2))'p'`
# calculate average $3 while $2 above threshold
awk -F ',' 'BEGIN {sum=0;num=0} {if($2>threshold){sum+=$3;num++}} END{print sum/num}' -v threshold=$th $file
非常简单吧~