Issue
I'm trying to select specific columns (2 and 3) from command output, where the column separator is also present in the text. Consider this example text (fictional part of nmcli
output):
SSID BSSID RSSI CHANNEL
Something 12:34:56:78:98:ab -10 1
Something guest a2:34:56:78:98:ab -10 2
Network b2:34:56:78:98:ab -20 3
Public network c2:34:56:78:98:ab -30 4
Columns are split by single spaces, but there are also single spaces in the SSIDs. Without those, it's an easy solution using cut
.
In GNU sed
, I somehow made it work using \w\s\w
to replace spaces in SSIDs with underscores:
sed -e 's/\w\s\w/_/g'
But that doesn't work on the Mac. So my second best idea is splitting by position. Is it possible to:
- determine the position of BSSID, RSSI and CHANNEL strings in first line
- cut between BSSID_pos and RSSI_pos (1) and RSSI_pos - CHANNEL (2)
Is it there a smarter way? Those spaces in SSIDs also confuse awk, so
awk '{print $2 " " $3}'
doesn't work in those lines.
--- EDIT --- The problem is, there are also spaces in the last field when using on data produced by MacOS airport -s, so I can't go from back either:
SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
AA1 d8:12:b6:b8:10:d5 -92 9 Y -- WPA(PSK/AES/AES) RSN(PSK/AES/AES)
XXXXX Guest ba:11:e4:4a:01:71 -90 6 Y -- RSN(PSK/AES/AES)
Netwrrk 5a:99:68:b7:0d:ca -89 36 Y DE RSN(PSK/AES/AES)
T-3_7edb87 64:11:ea:7e:db:87 -87 11 Y -- WPA(PSK/AES,TKIP/TKIP) RSN(PSK/AES,TKIP/TKIP)
abcde 58:22:8f:c6:7c:de -86 6 Y -- RSN(PSK/AES/AES)
abcde 64:66:88:3f:b2:9a -78 8 Y -- RSN(PSK/AES,TKIP/TKIP)
ababa 74:ac:77:b1:e3:59 -74 6 Y -- RSN(PSK/AES/AES)
Apologies for two questions in one; thank you all!
Solution
How about using GNU grep
here(tested in RedHat), which will simply catch the needed string by using regex in it. Written and tested with shown samples Only.
Here is the Online demo for used regex.
grep -oP '(?<=\s)[0-9a-f]{2}(:[0-9a-f]{2}){5}\s+-?[[:digit:]]{2}' Input_file
OR in perl
try following (thanks to @tripleee for this):
perl -nle 'm/(?<=\s)[0-9a-f]{2}(:[0-9a-f]{2}){5}\s+-?[[:digit:]]{2}/ and print "$&"' Input_file
Answered By - RavinderSingh13 Answer Checked By - Cary Denson (WPSolving Admin)