Bash – Multiline variable with spaces, adding each line to an array

bashquotingshell

I have a problem I can't seem to figure out..
I'm making a script I will run in OSX to help me stay connected to open wifi hotspots.
I am reading all surrounding wifi networks into a muntiline variable.
These wifi networks have sometimes spaces in their names, which presents a problem for me.
I want to loop through this multiline variable that contains spaces and add each line as it is to a new array cell.
Adding my code below.
Anyone that can help me sort this out?

#Looks for available networks
netarray=(`airport -s |
awk '$NF=="NONE"{print}' | 
cut -c 1-32,51-54 | 
sort -r -n -k 1.34,1.36 | 
awk '{$NF=""; print}' | 
sed -e 's/ $//' `)

#Test text added here
echo "netarray below"
echo "$netarray"
echo "cell 0 below"
echo ${netarray[0]}
echo "cell 1 below"
echo ${netarray[1]}
echo ""
#Until here

This gives me this output (See that TPE Free is cut):

netarray below
TPE Free HP-Print-93-LaserJet 1102
cell 0 below
TPE
cell 1 below
Free

airport -s gives me this output currently:

                        SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                   fuck this 00:1f:1f:27:e4:ac -79  11      N  -- WEP
                       BigA2 34:08:04:c3:9a:14 -85  11      N  US WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP) 
                    TPE Free 78:cd:8e:a6:ff:30 -90  10      N  T9 NONE
                     CHT3826 b0:b2:dc:c3:91:77 -82  11      Y  -- WPA2(PSK/AES/AES) 
                          KK 00:e0:4c:ac:e1:05 -84  11      N  -- WEP
                 perfectidea f8:1a:67:53:eb:da -79  9,-1    Y  -- WPA2(PSK/AES/AES) 
                   tfb_18_28 b0:c7:45:ac:18:98 -87  7,+1    Y  -- WPA2(PSK/AES,TKIP/TKIP) 
   HP-Print-93-LaserJet 1102 08:3e:8e:7c:04:93 -86  6       N  -- NONE
                   SG-OFFICE 74:d0:2b:dc:67:64 -80  6       Y  -- WPA2(PSK/AES/AES) 
                      holo15 b8:55:10:bd:3e:70 -82  5,-1    Y  -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES) 
             TOTOLINK N300RA 78:44:76:dd:bb:b4 -82  5,+1    Y  -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES) 
               ezstay 15F-28 78:44:76:dc:f1:a0 -85  2,+1    Y  KR WPA(PSK/AES/AES) WPA2(PSK/AES/AES) 
                      12F-32 20:10:7a:9f:d9:c1 -59  1       Y  -- WPA(PSK/AES,TKIP/TKIP) 

The awk on airport command just cuts out the names of the Open networks and puts them on separate rows.

Best Answer

mapfile -t ssids < <(
    airport -s | 
    sed -E '
        1d
        /NONE$/!d
        s/^[[:blank:]]*(.+) [[:xdigit:]:]{17} (...).*/\2 \1/
    ' | 
    sort -k1,1nr | 
    cut -d " " -f2-
)
printf "%s\n" "${ssids[@]}"
HP-Print-93-LaserJet 1102
TPE-Free

adjusting for bash v3

ssids=()
while read -r line; do ssids+=("$line"); done < <(
    airport -s | 
    sed -E '
        1d
        /NONE$/!d
        s/^[[:blank:]]*(.+) [[:xdigit:]:]{17} (...).*/\2 \1/
    ' | 
    sort -k1,1nr | 
    cut -d " " -f2-
)
printf "%s\n" "${ssids[@]}"
Related Question