I have an array:
list[0]=.abc.com
list[1]=.dd.eu
list[3]=.ww.bb.com
list[4]=.abc.bgs.eu
i want to remove all items from array that ends with ".com"
You can do:
s='.com'
echo "${list[@]/*$s/}"
.dd.eu .abc.bgs.eu
Or else to store resulting array in a new array:
s='.com'
read -ra arr <<< "${list[@]/*$s/}"
declare -p arr
declare -a arr='([0]=".dd.eu" [1]=".abc.bgs.eu")'
Simple approach - make a new array containing the elements you're interested in keeping:
new_list=()
for i in "${list[@]}"; do [[ $i != *.com ]] && new_list+=( "$i" ); done
You can try this:
This stores array results in a different array.
declare -a pattern=( ${list[@]/*.com/} )
echo ${pattern[@]}
Here list is your array
list[0]=.abc.com
list[1]=.dd.eu
list[3]=.ww.bb.com
list[4]=.abc.bgs.eu
and pattern is the resulting array with the following output:
.dd.eu .abc.bgs.eu