百分位(P95,P99)统计awk脚本

通过awk脚本对数据进行统计分布,查看百分位数据,平均值,方差

示例

cat cost.txt | sort -n | uniq -c | awk -f cal.awk```

结果

cost.txt 文件内容,是需要统计的数值

300
300
100
200
500
700
1000

脚本内容

#! /usr/bin/awk -f
{variance=0;sumCount+=$1;sumCost+=($2*$1);count[NR]=$1;cost[NR]=$2}
END { staticTotal[0]=50;
    staticTotal[1]=66;
    staticTotal[2]=80;
    staticTotal[3]=85;
    staticTotal[4]=90;
    staticTotal[5]=95;
    staticTotal[6]=98;
    staticTotal[7]=99;
    staticTotal[8]=100;
    staticFlag[0]=1;
    staticFlag[1]=1;
    staticFlag[2]=1;
    staticFlag[3]=1;
    staticFlag[4]=1;
    staticFlag[5]=1;
    staticFlag[6]=1;
    staticFlag[7]=1;
    staticFlag[8]=1;
    printf "%3s  %10s  %15s %15s\n", "static", "costt", "count", "diffPre";
    averageCost = sumCost/sumCount;
    for(i=1; i <=length(count); i++) {
        diff = (cost[i] - averageCost);
        variance += (diff*diff*count[i]/(sumCount-1));
        #printf("diff %s, variance %s, count[%s]: %s, cost[%s]: %s \n", diff, variance, i, count[i], i, cost[i]);
        countTotal += count[i];
        for (j=0; j <length(staticTotal); j++) {
        if (countTotal >= sumCount*staticTotal[j]/100) if (staticFlag[j]==1) {
            staticFlag[j]=sprintf("P%-3s  %10s %15s %15s", staticTotal[j],cost[i],countTotal, countTotal - countTotalPre);             countTotalPre = countTotal;
        }
    }
    };

for( i=0;i<length(staticFlag);i++) print staticFlag[i];
printf "count total: %s\n", sumCount, countTotal;
printf "average cost: %s \n", averageCost;
printf "variance cost: %s \n", variance;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值