Bash -


declare -a uplinks

# Iterate through each file in folder extra
for file in $(ls -1 ../extra); do

        # Declare an array uplinks
        declare -a uplinks


        # For current device $filepath list ports with more than 1 occurence of MAC and store on array uplinks
        for uplink in $(cat $filepath | awk '{ print $3 }' | grep 'gi\|te\|Po' | sort -h | uniq -c | sed s/^[[:blank:]]*//g | grep -v ^1" " | awk '{ print $2 }'); do

                # Append to array


        echo Switch export file: ../extra/$file

        # Separate each item from array by \| and remove \| from beginning of line.
        # This '\|gi26\|gi27\|gi28' becomes this 'gi26\|gi27\|gi28'
        filter=$(printf '\|%s' "${uplinks[@]}" | sed s/^\\\\\|//g)

        echo Uplink ports filtered out from switch: \'$filter\'

        # The grep -v takes $w a list of uplink ports for OR rule
        eval cat $filepath | grep -v $filter

        #Clear array for next iteration

        # Search Switch and store, search Uplink and store, search for $1(MAC) and found print them all
done | awk '/^Switch/ {switch_=$0} /^Uplink/ {uplink_=$0} /'"$1"'/ {print switch_ "\n" uplink_ "\n" $0}'