本文主要是介绍awk求和、平均值、最大值与最小值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
统计最后耗时的平均值、求和、最小值与最大值
cat client.log文件
[test@GROUPC udp_test_client]$ cat client.log | grep 耗时 | more
2018-11-15 21:40:51.818 INFO 6031 --- [ool-1-thread-86] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 IDf752c4c6-f6cc-4997-a263-d4effc386eb3,耗时291
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-86] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID2a048324-8a0d-4e00-a336-f844807c4484,耗时286
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-87] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID76a87369-8b63-4163-90e8-4ff5c5b0fd8f,耗时294
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-88] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 IDf726aa86-4777-44d2-8814-0098ae8ab2f9,耗时287
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-87] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID5dde22d9-d9e2-43e6-b0d7-d3188c22c00a,耗时290
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-88] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID918868b2-cfef-4e5a-8e5f-92322582bc61,耗时272
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-87] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID7f7414a5-18ff-44e4-a7b7-632ef6484cb1,耗时283
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-86] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID4214ee33-e8d1-40a2-9648-52f389dbf232,耗时281
2018-11-15 21:40:51.819 INFO 6031 --- [ool-1-thread-88] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID9dcfb42e-dfcd-4c8e-8ed2-0a309cb97b63,耗时269
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-87] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID9e61cb7e-6495-4723-a9e5-89eafa9218c0,耗时268
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-87] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 IDf455a85d-35fe-4d5b-9b72-b070967e7be0,耗时251
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-88] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 IDdeab63ae-4f9e-4cab-978f-52f57706bd12,耗时253
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-86] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID6b8621a3-db75-41aa-9b69-a6735a851a47,耗时266
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-90] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID23b0879f-3a4d-47aa-9dda-94a4bbee56ca,耗时271
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-89] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 IDeb11797b-9cf3-4a32-849d-b7902853e311,耗时271
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-89] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID3742ad2a-9d9e-4077-8967-02af6e134edd,耗时260
2018-11-15 21:40:51.821 INFO 6031 --- [ool-1-thread-89] c.y.nj.exchange.driver.ExchangeReceiver : #### 收到响应数据 ID83e2ccaf-9999-4807-8fd8-189b6f2c5d7d,耗时271
求和与平均值
cat client.log | grep 耗时 | awk '{print $NF}' | awk -F'耗时' '{sum+=$NF;count+=1} END{print "SUM="sum,"AVG="sum/count}'
求最小值
cat client.log | grep 耗时 | awk '{print $NF}' | awk -F'耗时' 'BEGIN {min = 9999999999}{if ($NF+0 < min+0) min=$NF} END {print "MIN="min}'
求最大值
cat client.log | grep 耗时 | awk '{print $NF}' | awk -F'耗时' 'BEGIN {max = 0}{if ($NF+0 > max+0) max=$NF} END {print "MAX="max}'
这篇关于awk求和、平均值、最大值与最小值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!