本文主要是介绍linux shell-awk样例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需求:
从 hive-server2.log 中截取所有当天日志,并统计其时间的平均值和时长最长的时间
hive-server2.log
2024-05-01 12:00:00 INFO [HiveServer2] Starting query execution
2024-05-01 12:00:01 INFO [HiveServer2] Query executed successfully in 5 seconds
2024-05-01 12:00:08 INFO [HiveServer2] Query result returned to client
实现:
hive.sh
#!/bin/bashawk '{print $2}' hive-server2.log |tr -d ":" | awk '{sum+=$1} END {print "avg:",sum/NR}' >>/root/hive.outputawk '{print $2}' hive-server2.log |tr -d ":" | awk '{max = 0} {if ($1+0 > max+0) max=$1} END {print "Max: ",max}' >>/root/hive.output
验证:
[root@logstash ~]# sh hive.sh
[root@logstash ~]# cat hive.output
avg: 120003
Max: 120008
[root@logstash ~]#
这篇关于linux shell-awk样例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!