0

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

"MyNetworkSSID"

FreeSoftwareServers
  • 2,482
  • 10
  • 37
  • 57

3 Answers3

1

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

$ sudo iwlist wlan0 scan | awk -F ':' '/ESSID:/ {print $2;}'
"BTWifi-with-FON"
"BTHub5-FTQN"
"BTWifi-X"
"4GEEOnetouchY800z_2DEB"

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).

garethTheRed
  • 33,289
  • 4
  • 92
  • 101
  • 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 – FreeSoftwareServers Dec 21 '15 at 08: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 :-) – garethTheRed Dec 21 '15 at 08:37
0

Try

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.

cremefraiche
  • 535
  • 2
  • 8
0

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
FreeSoftwareServers
  • 2,482
  • 10
  • 37
  • 57