数组找最大值linux,linux shell实现求一个多维数组中的最大和最小值

同事发了一道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/bash

echo "the file is :"

cat 99shu

max=0

min=999999

line=1

dnum=$(cat 99shu| wc -l)

while (($line<=$dnum))

do

for i in $(cat 99shu|head -"$line")

do

((max

((min>$i))&&min=$i

done

let ++line

done

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 MIN

echo "the numbers is:"

cat 99shu

mnum=0

min=99999

while read line

do

declare -a arr=($line)

lnum=$(echo $line | wc -w)

i=0

while (( $i

do

(($mnum

(($min>${arr[i]})) && min=${arr[i]}

let ++i

done

done < 99shu

echo "the max number is $mnum"

echo "the min number is $min"

实现3,强大的awk

#! /bin/bash

echo "the MAX number is: $( cat 99shu | awk '{for(i=1;i<=NF;i++)if(max

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/bash

min=$(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"

时间: 2016-08-13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值