skip to Main Content

I have an array from with numbers from 1 to 100:

array=$(seq 100)

My task is to exclude range from 60 to 80.

3

Answers


  1. You can use parameter expansion with the offset/length specification.

    #! /bin/bash
    arr=({1..100})
    exclude_from=60
    exclude_to=80
    echo "${arr[@]:0:exclude_from-1}" "${arr[@]:exclude_to}"
    
    Login or Signup to reply.
  2. Something like this maybe?

    #!/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 '%dn' "$i"
    done
    

    Or create a temp array to build up the elements.

    #!/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
    
    Login or Signup to reply.
  3. An arithmetic test condition

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

    However, to remove those elements from an array

    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
    

    outputs

    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 ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................^^^^^^^^^^^^^^^^^^^
    

    And

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

    excludes 60-80

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search