Extract Maximum and minimum value using awk

前端 未结 3 1390
遇见更好的自我
遇见更好的自我 2021-01-01 05:15

How to find maximum and minimum value from the below table using awk command.

20 90 60 30
55 75 80 85
10 15 99 95
55 95 70 20
9  35 85 75
         


        
3条回答
  •  -上瘾入骨i
    2021-01-01 05:56

    with gnu awk:

    awk '{for(x=1;x<=NF;x++)a[++y]=$x}END{c=asort(a);print "min:",a[1];print "max:",a[c]}'
    

    output:

    min: 9
    max: 99
    

    without awk:

    xargs -n1|sort -n|head or tail -1
    

    e.g.

    min:

    kent$  echo "20 90 60 30
    55 75 80 85
    10 15 99 95
    55 95 70 20
    9  35 85 75"|xargs -n1|sort -n|head -1
    9
    

    max:

    kent$  echo "20 90 60 30
    55 75 80 85
    10 15 99 95
    55 95 70 20
    9  35 85 75"|xargs -n1|sort -n|tail -1
    99
    

    you can of course xargs -n1|sort -n then pipe to awk to pick first and last and print in one shot.

提交回复
热议问题