linux 中 计算一列数值中最大值、最小值、及最大值和最小值之差
01、先排序,然后取首尾(数据大时不适用)
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文件 8 3 39 28 2 4 6 [root@pc1 test1]# sort -n a.txt | head -n 1 ## 最小值 2 [root@pc1 test1]# sort -n a.txt | tail -n 1 ## 最大值 39

002、利用awk循环判断
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 8 3 39 28 2 4 6 ## 输出最小值 [root@pc1 test1]# awk '{if(NR == 1) {min = $1}; if($1 < min) {min = $1}} END {print min}' a.txt 2 ## 输出最大值 [root@pc1 test1]# awk '{if(NR == 1) {max = $1}; if($1 > max) {max = $1}} END {print max}' a.txt 39

003、
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试数据 8 3 39 28 2 4 6 ## 输出最大值和最小值之差 [root@pc1 test1]# sort -n a.txt | head -n 1 | paste - <(sort -rn a.txt | head -n 1) | awk '{print $2 - $1}' 37

。

浙公网安备 33010602011771号