Removing multiple elements from a bash array based on different patterns -
so have this:
interfaces=($(ip -o link show | awk -f': ' '{print $2}'))
giving me array of interfaces, want remove loopback , tun adapters array.
interfaces=(${interfaces[@]/lo/}) interfaces=(${interfaces[@]/tun*/})
works, there way in single line?
probably super easy , derp, i'd understand how expression works (google gave me 47239432 examples of above not 1 explained how worked or gave example of using more 1 expression @ time).
i know can done awk command, both solution awk , 1 array filter appreciated :)
my apologies if stupid question.
there many ways this. 1 example:
# arr=(lo tun0 net1 net2) # echo ${arr[@]} lo tun0 net1 net2 # arr=( $( printf '%s\n' ${arr[@]} | egrep -v '^(lo|tun)' ) ) # echo ${arr[@]} net1 net2 #
and can exclude these interfaces @ beginning:
interfaces=( $(ip -o link show | awk -f': ' '$2 !~ /^(lo|tun)/ {print $2}') )
Comments
Post a Comment