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

首頁 > 系統 > Linux > 正文

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

2019-10-26 18:49:03
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 酒泉市| 双江| 常熟市| 祁门县| 宁津县| 呼伦贝尔市| 舒城县| 合作市| 平定县| 溆浦县| 侯马市| 庐江县| 文成县| 苏尼特右旗| 镇江市| 南昌市| 天气| 噶尔县| 华容县| 民乐县| 阜平县| 蒲江县| 广平县| 永春县| 会同县| 格尔木市| 逊克县| 平江县| 伊春市| 宜宾市| 陆川县| 中江县| 滨海县| 博湖县| 霸州市| 全椒县| 东宁县| 元朗区| 托克逊县| 榆中县| 铜川市|