- 论坛徽章:
- 5
|
本帖最后由 blackold 于 2013-03-27 11:46 编辑
测试:- $ wc -l urfile
- 297620 urfile
复制代码- $ time awk '!c[$1]++{h[$1]=$1" "$2" "$3;max[$1]=min[$1]=$4;m[$1]=n[$1]=$5;}{if($4>max[$1]){max[$1]=$4;m[$1]=$5;}else if($4<min[$1]){min[$1]=$4;n[$1]=$5;}a[$1]+=$4;b[$1]+=$5;}END{for(i in c){if(c[i]>=3){a[i]-=max[i]+min[i];b[i]-=m[i]+n[i];c[i]-=2;}print h[i],a[i]/c[i],b[i]/c[i];}}' urfile
- Ken Black Driver 84.67 47.33
- Anne Red Student 60.86 105.27
- Peter Blue Teacher 77.9499 50.9545
- Sue Yellow Worker 114.58 66.3602
- awk urfile 4.58s user 0.06s system 99% cpu 4.641 total
复制代码- $ time awk '{x=$1;p[x]=$1" "$2" "$3;if(!c[x]++)for(n=0;n++<2;)p[x,n]=p[x,n+2]=p[x,n+4]=$(n+3);else{p[x,1]+=$4;p[x,2]+=$5;if(p[x,3]<$4){p[x,3]=$4;p[x,4]=$5}if(p[x,5]>$4){p[x,5]=$4;p[x,6]=$5}}}END{for(x in c){if(c[x]<=2){x4=p[x,1]/c[x];x5=p[x,2]/c[x]}else{x4=(p[x,1]-p[x,3]-p[x,5])/(c[x]-2);x5=(p[x,2]-p[x,4]-p[x,6])/(c[x]-2)}print p[x],x4,x5}}' urfile
- Ken Black Driver 84.67 47.33
- Anne Red Student 60.86 105.27
- Peter Blue Teacher 77.9499 50.9545
- Sue Yellow Worker 114.58 66.3602
- awk urfile 7.50s user 0.06s system 98% cpu 7.641 total
复制代码- $ time awk '{a[$1]=a[$1]!=""?a[$1]" "$4:$4;b[$1,$4]=$5;NF-=2;c[$1]=$0}END{for(i in a){k=split(a[i],d," ");asort(d,e);if(k==1)print c[i],e[1],b[i,e[1]];if(k==2){m=(e[1]+e[2])/2;n=(b[i,e[1]]+b[i,e[2]])/2;print c[i],m,n}if(k>=3){for(j=2;j<k;j++){m+=e[j];n+=b[i,e[j]]}print c[i],m/(k-2),n/(k-2)}m=n=0}}' urfile
- .... 还在运行中
复制代码 |
|