linux 中 awk命令输出一列数据中的最小值、最大值

 

001、

复制代码
root@DESKTOP-1N42TVH:/home/test5/test/test# cat a.txt
5 8
2 7
9 30
1 10
3 6
7 5
root@DESKTOP-1N42TVH:/home/test5/test/test# awk '{if(NR == 1) {min = $1} else {if($1 < min) {min = $1}}} END {print min}' a.txt
1                                          ## 输出第一列的最小值
root@DESKTOP-1N42TVH:/home/test5/test/test# awk '{if(NR == 1) {max = $1} else {if($1 > max) {max = $1}}} END {print max}' a.txt
9                                          ## 输出第一列的最大值
复制代码

 

002、

复制代码
root@DESKTOP-1N42TVH:/home/test5/test/test# ls
a.txt
root@DESKTOP-1N42TVH:/home/test5/test/test# cat a.txt
5 8
2 7
9 30
1 10
3 6
7 5
root@DESKTOP-1N42TVH:/home/test5/test/test# awk '{if(NR == 1) {min = $1; max = $2} else {if($1 < min) {min = $1}; if($2 > max) {max = $2}}} END {print min, max}' a.txt
1 30                                        ## 同时输出第一列的最小值、第二列的最大值
复制代码

 

posted @   小鲨鱼2018  阅读(744)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-07-19 c语言中打印浮点数
2021-07-19 linux系统中diff命令
点击右上角即可分享
微信分享提示