【问题标题】:Bash : Exclude range from arrayBash:从数组中排除范围
【发布时间】:2021-06-18 23:52:05
【问题描述】:

我有一个从 1 到 100 的数组:

array=$(seq 100)

我的任务是排除 60 到 80 的范围。

【问题讨论】:

  • 你确定它是一个数组吗?看起来更像是一个字符串。

标签: bash centos


【解决方案1】:

您可以将参数扩展与偏移/长度规范一起使用。

#! /bin/bash
arr=({1..100})
exclude_from=60
exclude_to=80
echo "${arr[@]:0:exclude_from-1}" "${arr[@]:exclude_to}"

【讨论】:

  • 我需要在我的任务中使用循环(
  • 为什么?这要快得多。
  • 这是任务的条件
【解决方案2】:

算术测试条件

for n in {1..100}; do
    (( 60 <= n && n <= 80 )) && continue
    echo $n
done

但是,要从数组中删除这些元素

ary=({1..100})
# note that number 1 is stored in _index_ 0
for ((n=60; n <= 80; n++)); do
    unset "ary[$((n-1))]"
done
declare -p ary

输出

declare -a ary=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5" [5]="6" [6]="7" [7]="8" [8]="9" [9]="10" [10]="11" [11]="12" [12]="13" [13]="14" [14]="15" [15]="16" [16]="17" [17]="18" [18]="19" [19]="20" [20]="21" [21]="22" [22]="23" [23]="24" [24]="25" [25]="26" [26]="27" [27]="28" [28]="29" [29]="30" [30]="31" [31]="32" [32]="33" [33]="34" [34]="35" [35]="36" [36]="37" [37]="38" [38]="39" [39]="40" [40]="41" [41]="42" [42]="43" [43]="44" [44]="45" [45]="46" [46]="47" [47]="48" [48]="49" [49]="50" [50]="51" [51]="52" [52]="53" [53]="54" [54]="55" [55]="56" [56]="57" [57]="58" [58]="59" [80]="81" [81]="82" [82]="83" [83]="84" [84]="85" [85]="86" [86]="87" [87]="88" [88]="89" [89]="90" [90]="91" [91]="92" [92]="93" [93]="94" [94]="95" [95]="96" [96]="97" [97]="98" [98]="99" [99]="100")
# ... note ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................^^^^^^^^^^^^^^^^^^^

for n in "${ary[@]}"; do echo $n; done
# or more concisely
printf '%d\n' "${ary[@]}"

不包括 60-80

【讨论】:

    【解决方案3】:

    可能是这样的吗?

    #!/usr/bin/env bash
    
    arr=({1..100})
    exclude=({60..80})
    
    exclude_pattern=$(IFS='|'; printf '%s' "@(${exclude[*]})")
    
    for i in "${arr[@]}"; do
      [[ $i == $exclude_pattern ]] && continue
      printf '%d\n' "$i"
    done
    

    或者创建一个临时数组来构建元素。

    #!/usr/bin/env bash
    
    arr=({1..100})
    exclude=({60..80})
    
    exclude_pattern=$(IFS='|'; printf '%s' "@(${exclude[*]})")
    
    for i in "${arr[@]}"; do
      [[ $i == $exclude_pattern ]] && continue
      included_arr+=("$i")
    done
    
    arr=("${included_arr[@]}")
    
    declare -p arr
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 2013-04-14
      • 2015-01-12
      相关资源
      最近更新 更多