Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I execute command

sudo iwlist wlan0 scan  | grep ESSID

resulting

ESSID:"DHS_3RD_FLOOR" ESSID:"MAXTA" ESSID:"MAXTA_5THWL" ESSID:"OPENSTACK" ESSID:"IOT" ESSID:"ved_opa" ESSID:"dlink" ESSID:"WifiFeazt"

But I want output as:(without ESSID:")

DHS_3RD_FLOOR MAXTA MAXTA_5THWL OPENSTACK IOT ved_opa dlink WifiFeazt

I googled but I have no idea how to do it. Thanks.

share|improve this question
    
Does sudo iwlist wlan0 scan | grep ESSID result is one line or multiple line? –  cuonglm Nov 28 at 7:27

4 Answers 4

up vote 6 down vote accepted

With GNU sed:

sed -r 's/(ESSID:|")//g'

or

sed 's/\(ESSID:\|"\)//g'

or

perl -pe 's/(?:ESSID:|")//g'

or in pure bash:

str=$(sudo iwlist wlan0 scan | grep ESSID)
str=${str//ESSID:/}
echo ${str//\"/}

Output:

DHS_3RD_FLOOR MAXTA MAXTA_5THWL OPENSTACK IOT ved_opa dlink WifiFeazt
share|improve this answer
    
Thanks. Worked. –  linux_inside Nov 28 at 7:14
    
Seeing you are on a roll: awk '{gsub(/ESSID:/,"")}1'... –  jasonwryan Nov 28 at 7:15

In awk, i would do like

$ .... | awk '{gsub(/ESSID:|"/,"")}1'
DHS_3RD_FLOOR MAXTA MAXTA_5THWL OPENSTACK IOT ved_opa dlink WifiFeazt
share|improve this answer
    
awk 'gsub(/ESSID:|"/,"")' ensures only lines that contained ESSID are printed(which means the grep is not needed) –  Jidder Nov 28 at 8:47

POSIXly:

sed -e 's/[^"]*"\([^"]*\)"/\1 /g'
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.