awk实现求和、平均、最大值和最小值的计算操作
准备和数据文件
[finance@master2-dev ~]$ cat data.txt 490898 1189235 20212 1494270 146515 29369 23563 563027 22976 127809 16813 551646 18858 18977
求和
[finance@master2-dev ~]$ cat data.txt|awk '{sum+=$1} END {print "Sum = ", sum}' Sum = 4714168
求平均
[finance@master2-dev ~]$ awk '{sum+=$1} END {print "Average = ", sum/NR}' data.txt Average = 336726
求最大值
[finance@master2-dev ~]$ awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}' data.txt Max= 563027
求最小值(min的初始值设置一个超大数即可)
[finance@master2-dev ~]$ awk 'BEGIN {min = 1999999} {if ($1<min) min=$1 fi} END {print "Min=", min}' data.txt Min= 1189235
最大最小值注意linux系统下比较的方法