我有一个文本文件,格式如下:
crelimit:300000,sos:300000
crelimit:300000,sos:120000
crelimit:300000,sos:10000
crelimit:300000,sos:400000
crelimit:300000,sos:600000
crelimit:300000,sos:700000
crelimit:100000,sos:800000
crelimit:200000,sos:800000
crelimit:300000,sos:1100000
crelimit:120000,sos:120000
我正在将脚本写入计算器总和减去第2列和第4列的值,条件如下:
cat ir7_temp.txt|sed 's/:/,/g'|cut -d"," -f2,4|awk 'BEGIN {
FS=",";
OFS=",";
}
(NR > 0){
percent = 100 * $2 / $1;
if (percent <= 20) {
a["0-20%"] += ($1-$2);
} else if (percent <= 30) {
a["21-30%"] += ($1-$2);
} else if (percent <= 50) {
a["31-50%"] += ($1-$2);
} else if (percent <= 60) {
a["51-60%"] += ($1-$2);
} else if (percent <= 70) {
a["61-70%"] += ($1-$2);
} else if (percent <= 80) {
a["71-80%"] += ($1-$2);
} else if (percent <= 90) {
a["81-90%"] += ($1-$2);
} else {
a["91-100%"] += ($1-$2);
}
}
END {
for (i in a) {
print i, a[i];
}
}'|sort > ir7_temp_resul1.txt
但我总和得到负值。你能帮忙检查脚本是否正确吗?或者任何建议写它更简单??
0-20%,9581263517
21-30%,2304516259
31-50%,2684412010
51-60%,571491884
61-70%,503126890
71-80%,223638210
81-90%,144111617
91-100%,-45135696
答案 0 :(得分:0)
您可以稍微简化代码
$ awk -F[,:] -v OFS=, '{a[int($4/$2)]+=($2-$4)}
END {for(k in a) print k*10"-"(k+1)*10-1"%",a[k]}' file
并且可能在后期处理中合并前两个桶。