zoukankan      html  css  js  c++  java
  • linux系统中awk命令求一列值的最大值、最小值、和及平均值

    1、最大值

    [root@centos7 test]# cat a.txt
    4 2 8
    6 4 9
    2 7 5
    3 5 7
    [root@centos7 test]# awk 'BEGIN{max = 0}{if($1 > max) max = $1}END{print max}' a.txt
    6
    [root@centos7 test]# awk 'BEGIN{max = 0}{if($2 > max) max = $2}END{print max}' a.txt
    7
    [root@centos7 test]# awk 'BEGIN{max = 0}{if($3 > max) max = $3}END{print max}' a.txt
    9

    2、最小值

    [root@centos7 test]# cat a.txt
    4 2 8
    6 4 9
    2 7 5
    3 5 7
    [root@centos7 test]# awk 'BEGIN{min = 999999999}{if($1 < min) min = $1}END{print min}' a.txt
    2
    [root@centos7 test]# awk 'BEGIN{min = 999999999}{if($2 < min) min = $2}END{print min}' a.txt
    2
    [root@centos7 test]# awk 'BEGIN{min = 999999999}{if($3 < min) min = $3}END{print min}' a.txt
    5

    3、求和

    [root@centos7 test]# cat a.txt
    4 2 8
    6 4 9
    2 7 5
    3 5 7
    [root@centos7 test]# awk '{sum += $1}END{print sum}' a.txt
    15
    [root@centos7 test]# awk '{sum += $2}END{print sum}' a.txt
    18
    [root@centos7 test]# awk '{sum += $3}END{print sum}' a.txt
    29

     4、求平均值

    [root@centos7 test]# cat a.txt
    4 2 8
    6 4 9
    2 7 5
    3 5 7
    [root@centos7 test]# awk '{sum += $1}END{print sum/NR}' a.txt
    3.75
    [root@centos7 test]# awk '{sum += $2}END{print sum/NR}' a.txt
    4.5
    [root@centos7 test]# awk '{sum += $3}END{print sum/NR}' a.txt
    7.25
  • 相关阅读:
    Linux
    bzoj 1834
    bzoj 1002 找规律(基尔霍夫矩阵)
    bzoj 1005 组合数学 Purfer Sequence
    bzoj 1601 最小生成树
    bzoj 1001 平面图转对偶图 最短路求图最小割
    bzoj 1192 二进制
    bzoj 1012 基础线段树
    bzoj 1044 贪心二分+DP
    bzoj 1011 近似估计
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14664632.html
Copyright © 2011-2022 走看看