I'm trying to parse the results of nmcli dev wifi
which produces a result like this:
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
Prk Infra 11 54 Mbit/s 99 ▂▄▆█
VIDEOTRON2255 Infra 11 54 Mbit/s 67 ▂▄▆_ WPA1 WPA2
a space Infra 6 54 Mbit/s 65 ▂▄▆_ WPA2
* TNCAP4D0B18 Infra 11 54 Mbit/s 52 ▂▄__
Initially I was just parsing using awk -F" "
which worked for almost all cases. I'm finding any wifi network with a space
in it throws this off completely.
So I instead try using two spaces instead of one this didn't produce the results I expected. How can I consistently parse the columns in the above output?
Current script is something like this:
nmcli dev wifi | sed 's/\*//g' > /tmp/scan
networks=$(cat /tmp/scan | awk -F" " '{print $1}' | sed '1d')
# ...
bars=$(cat /tmp/scan | awk -F" " '{print $6}' | sed '1d')
awk -F'\t'
?nmcli
, you can avoid needing to parse the output by specifying exactly what fields you want (and in what order) e.g.networks=$(nmcli -t -f ssid dev wifi)
. There's also an option to print in a machine-parsable mode.--mode multiline
format if your version ofnmcli
supports it - which gives you colon-delimited key-value pairs out of the box