﻿ 使用awk统计最大值、最小值、平均值-LINUX_操作系统-脚本宝典

# 使用awk统计最大值、最小值、平均值

## 使用awk统计最大值、最小值、平均值

1、使用sort排序，求最大值、最小值，使用for循环求平均值：
grep RenewalCheckIbatisDAO.*getHistoryPolicyListByLicenceNo pafa.log |awk -F'time=' '{print \$2}'|awk -F'ms' '{a[NR]=\$1}  END{b=0;c=a[1];d=a[1];for (i in a){b=b+a[i];if(c>a[i])c=a[i];if (d<a[i]) d=a[i]};print "Min:"c,"Max:"d,"Avg:"b/NR}'

2、使用for循环+if判断求最大值、最小值、平均值：
grep RenewalCheckIbatisDAO.*getHistoryPolicyListByLicenceNo pafa.log.2013-05-19 |awk -F'time=' '{print \$2}'|awk -F'ms' '{a[NR]=\$1}  END{b=0;c=a[1];d=a[1];for (i in a){b=b+a[i];if(c>a[i])c=a[i];if (d<a[i]) d=a[i]};print "Min:"c,"Max:"d,"Avg:"b/NR}'

[[email protected] pa18shopauto]\$ wc -l pafa.log.2013-05-19
5038206 pafa.log.2013-05-19

[[email protected] pa18shopauto]\$ time(grep RenewalCheckIbatisDAO.*getHistoryPolicyListByLicenceNo pafa.log.2013-05-19 |awk -F'time=' '{print \$2}'|awk -F'ms' '{print \$1| "sort -n"}' | awk '{a[NR]=\$1}  END{for (i in a)b=b+a[i];print "Min:"a[1],"Max:"a[NR],"Avg:"b/NR}')
Min:0 Max:1029 Avg:45.0941
real 0m2.450s
user 0m0.893s
sys 0m1.613s

[[email protected] pa18shopauto]\$ time(grep RenewalCheckIbatisDAO.*getHistoryPolicyListByLicenceNo pafa.log.2013-05-19 |awk -F'time=' '{print \$2}'|awk -F'ms' '{a[NR]=\$1}  END{b=0;c=a[1];d=a[1];for (i in a){b=b+a[i];if(c>a[i])c=a[i];if (d<a[i]) d=a[i]};print "Min:"c,"Max:"d,"Avg:"b/NR}')
Min:0 Max:1029 Avg:45.0941
real 0m2.432s
user 0m0.895s
sys 0m1.571s

Tags：

﻿
<