国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

linux shell實現求一個多維數組中的最大和最小值

2020-07-27 19:01:24
字體:
來源:轉載
供稿:網友

同事發了一道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"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高淳县| 昂仁县| 定西市| 滕州市| 衡水市| 肥东县| 赣州市| 普兰店市| 肥东县| 神池县| 汕头市| 竹北市| 疏勒县| 大方县| 无为县| 延安市| 茌平县| 东乡族自治县| 吉木乃县| 蓬溪县| 定远县| 武穴市| 卓尼县| 建水县| 萨迦县| 辰溪县| 泸溪县| 四平市| 嘉义市| 玉林市| 高安市| 土默特右旗| 福贡县| 沅陵县| 措美县| 杂多县| 萨嘎县| 阳西县| 临湘市| 新蔡县| 穆棱市|