So my goal is to answer my other question I am working on. Which is Connect to Open Wifi. Currently I am getting close, as I know there is only one Wifi connection, I would like to export the results of

sudo iwlist wlan0 scan | grep ESSID

to a text file. The output currently is:

ESSID: "MyNetworkSSID"  # Which would end up being in the file

What I want is a text file that says only


3 Answers 3


Don't bother with grep. Pipe it directly to awk as follows:

$ sudo iwlist wlan0 scan | awk -F ':' '/ESSID:/ {print $2;}'

This carries out a regexp search for ESSID: and the splits that line on a colon (-F ':') after which it prints the second element of that split (print $2).

Or, pipe it through perl:

$ sudo iwlist wlan0 scan | perl -nle '/ESSID:(.*)$/ && print $1'

This causes perl to run the command (-e) on each line of the input (-n) and adds a line feed at the end of each line (-l). The command is a regex which searches for ESSID: and captures the remaining line ((.*)$). On finding this match, it prints the capture (&& print $1).

  • 1
    can you elaborate on why not to use grep? awk seems to be the agreed method, but grep worked fine as well for me Commented Dec 21, 2015 at 8:20
  • Simply because it's one less command? If you use grep you end up piping it's output through something else anyway (or grep again). Why not ditch grep altogether and pipe it directly to something that is more capable? I didn't mean to sound anti-grep :-) Commented Dec 21, 2015 at 8:37

Hazar, this worked, but it is more case specific as it has to do with the double quotes vs excluding the word ESSID.

sudo iwlist wlan0 scan | grep ESSID | grep -o '"[^"]\+"' >> essid.txt


sudo iwlist wlan0 scan | grep -i essid | awk -F'"' '{ print $2 }' >> essid.txt

This pipes the output of grep to awk, which uses the delimiter of " and only prints the field with ESSID.

  • that just showed a number, but I only copy and pasted, didn't troubleshoot as I figured it out! Thank you tho Commented Dec 21, 2015 at 6:28
  • @FreeSoftwareServers I didn't actually test this, I was assuming the fields were tab separated like OP shows. Answer is now adjusted. Commented Dec 21, 2015 at 6:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .