Using GNU grep
:
$ sudo iwlist wlan0 scan | grep -oP ':"\K[^"]+?(?=")'
DHS_3RD_FLOOR
MAXTA
MAXTA_5THWL
OPENSTACK
IOT
ved_opa
dlink
WifiFeazt
The -o
flag means "print only the matching portion of the line", when more than one matches are found, all will be printed. The -P
enables PCREs which give us \K
("ignore everything matched up to here") and lookaheads. These allow to match depending on the following characters but without including said characters in the match itself. So, the regular expression means "look for :"
and discard it, then match as many non-"
as possible until the next "
".
To get everything on one line, pass it through tr
:
$ sudo iwlist wlan0 scan | grep -oP ':"\K[^"]+?(?=")' | tr '\n' ' '
DHS_3RD_FLOOR MAXTA MAXTA_5THWL OPENSTACK IOT ved_opa dlink WifiFeazt $
Or, to get a trailing newline:
$ echo $(sudo iwlist wlan0 scan | grep -oP ':"\K[^"]+?(?=")' | tr '\n' ' ')
DHS_3RD_FLOOR MAXTA MAXTA_5THWL OPENSTACK IOT ved_opa dlink WifiFeazt
sudo iwlist wlan0 scan | grep ESSID
result is one line or multiple line? – cuonglm Nov 28 at 7:27