linux shell实现求一个多维数组中的最大和最小值
linux  /  管理员 发布于 8年前   408
同事发了一道shell题,是求一个多维数组中的最大和最小值
如文件 99file:
33 55 23 56 99
234 234 545 6546 34
11 43 534 33 75
43 34 76 756 33
343 890 77 667 55
我的实现之一:
#! /bin/bashecho "the file is :"cat 99shumax=0min=999999line=1dnum=$(cat 99shu| wc -l)while (($line<=$dnum))dofor i in $(cat 99shu|head -"$line") do ((max<$i))&&max=$i ((min>$i))&&min=$i donelet ++linedone echo "the max number is: $max"echo "the min number is : $min"
结果:
the max number is: 6546
the min number is : 11
实现之二:
#! /bin/bash# echo the MAX and the MINecho "the numbers is:"cat 99shumnum=0min=99999while read line dodeclare -a arr=($line)lnum=$(echo $line | wc -w)i=0while (( $i<$lnum ))do(($mnum<${arr[i]})) && mnum=${arr[i]}(($min>${arr[i]})) && min=${arr[i]}let ++idonedone < 99shuecho "the max number is $mnum"echo "the min number is $min"实现3,强大的awk
#! /bin/bashecho "the MAX number is: $( cat 99shu | awk '{for(i=1;i<=NF;i++)if(max<$i) max=$i;print max}'|tail -1)"echo "eht MIN number is: $( cat 99shu | awk '{min=999999;for(i=1;i<=NF;i++)if(min>$i)min=$i;print min}'|sort|head -1 )"实现4:
#!/bin/bashmin=$(cat 99shu | tr "\t" "\n"|tr " " "\n"|sort -n|uniq|grep -v "^$"|head -1)max=$(cat 99shu | tr "\t" "\n"|tr " " "\n"|sort -n|uniq|grep -v "^$"|tail -1)echo "The MAX number is $max"echo "The MIN number is $min"
test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
