当前位置: 动力学知识库 > 问答 > 编程问答 >

awk - ksh calculate percentage of a colum where values < 2

问题描述:

I have a large file where the fourth column contains decimal based values. How can I calculate the percentage of records that are < 2 ?

test test test .001

test2 test2 test2 8.0993

test3 test3 test3 .00001

test4 test4 test4 1.001

网友答案:

This awk script will do it:

awk '{$4<2?l++:g++}END{printf "< 2 : %s %\n", l*100/(g+l); printf ">=2 : %s %", g*100/(g+l)}' your.file 

For better readability place it a file:

percent.awk:

{ $4<2 ? l++ : g++ }
END {
    printf "<  2 : %s %\n", l*100/(g+l)
    printf ">= 2 : %s %", g*100/(g+l)
}

and execute it like:

awk -f percent.awk your.file 
分享给朋友:
您可能感兴趣的文章:
随机阅读: