tags:

views:

202

answers:

3

I have very little experience with AWK, but it seems like the best tool for my purpose now.

I am trying to get a list of nearby BSSIDs by using the airport -s command in OS X. Output looks like this:

                        SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
 MyWireless 00:11:22:33:44:55 -85  64      N  US WPA(PSK/TKIP/TKIP) WPA2(PSK/TKIP/TKIP) 
 Your Wireless 66:77:88:99:00:11 -84  64      N  US WPA(PSK/TKIP/TKIP) WPA2(PSK/AES/TKIP) 

So clearly I'm looking for the second column. So I tried:

airport -s | awk '{print $2}'

And that works fine until I have an SSID with a space in its name. I've tried setting IFS to tab, FS to tab, nothing really seems to work.

I keep getting this:

00:11:22:33:44:55

Your

I am hoping eventually to get a simple list:

00:11:22:33:44:55

66:77:88:99:00:11

I know this is a one-line solution, so I'm really embarrassed that I even have to ask... Please be kind. :)

+4  A: 

Seems like a regexp might be better suited. Maybe just a simple egrep -o

airport -s | egrep -o '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'
Peter Kovacs
Better that the sed script I was fooling around with.
dmckee
works great - thanks!
Andrew J. Freyer
A: 

I'd be curious to see how you are setting FS to tab, because that ought to work if airport is outputting a tab between columns.

You can also use a regular expression as a field separator (at least in gawk). This will handle the case when there is one or more tabs between fields:

airport -s | awk -F'\t+' '{print $2}'
Dennis Williamson
A: 

easy, you just have to go through each field and check for the pattern

airport -s | awk 'NR>1{ for(i=1;i<=NF;i++){if($i~/^[0-9][0-9]:/){print $i}}}' 
ghostdog74