Help needed for using awk to parse a file to make array for bash script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Help needed for using awk to parse a file to make array for bash script
I am trying to write a script that will run "iwlist <interface> scanning >> iwlist-file."
Then I want to parse the file so that the ESSID, AP, encryption key off vs on, and signal strength all become variables that I will be able to use in the script.
I will have some preferred networks with special static vs dhcp connections that if that ESSID is available, a different script will run (say in same directory named by network) but if no network is found, it would prioritize the available networks with Encryption key off by signal strength and connect automatically to it.
The file I need to parse to obtain those variables would look something like this:
Thanks for your quick reply. That lists the available networks, but I want to be able to somehow associate the ESSID with its specific AP, key (off or on), and signal strength.
Maybe breaking up the file into separate temporary files in /tmp/ with that info? maybe awk array? I don't know how todo either and am quite a novice at scripting.
Can someone give me an explanation of the command?
Yes. The first part (-F '[ :=]+') sets your “field separator” to be one or more space, colon, or equal sign characters. The first command (/(ESS|Freq|Qual)/{ printf $3" " }) says that whenever awk encounters either ESS, Freq, or Qual, it will print the third field followed by a space (with no newline). The second command (/Encr/{ print $4 }) says that when awk encounters Encry, it will print the fourth field, followed by a newline. The result is that you get the output you want. If you wanted additionally to have the address printed out, you could add the command
Code:
/Cell/{printf substr($0,30)" "}
which means to print the entire line after the character at index 30 when awk encounters Cell.
Quote:
Originally Posted by tallmtt
Any suggestions are always welcome.
If all you need to do is sort the output, why not use the sort command. For example, suppose I had the following output:
Code:
# iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }'
"foo" 2.437 19/70 off
"bar" 2.437 9/70 on
"baz" 2.462 39/70 on
"qux" 2.437 56/70 off
"quux" 2.437 34/70 off
I want to sort this list first by the fourth field alphabetically (so off comes before on) and then by the third field numerically in descending order. So I could do:
Code:
# iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr
"qux" 2.437 56/70 off
"quux" 2.437 34/70 off
"foo" 2.437 19/70 off
"baz" 2.462 39/70 on
"bar" 2.437 9/70 on
The above should work as long as the quality field is a fraction with denominator 70 (since sort only looks at the initial numerical part). If you want something that will be smart enough to tell that 70/100 is less than 69/70, you will probably want awk to print a decimal for you instead of the number itself.
Hello. I found this older thread by searching via Google and it has been a great help to me as I learn scripting. However, I must be missing something, because I still don't see how these examples make the results available as variables. I'd like to take the results and put them into an Xdialog menu so that the user could select which network to connect to, then pass the results to the iwconfig command based on the user's selection. I find that if I use iwconfig, I can connect more reliably than via wicd or WiFi Radar, neither of which seem to work very well in my configuation.
I've purchased some books on bash scripting, but I'm at a wall on the variable array technique. I'd appreciate any advice.
I wish I searched for this thread instead of reinventing the wheel which took me weeks while holding a crying newborn. But it was great to learn the basics of awk and grep.
I am a recent migrant from Windows where I was using Netstumbler to align high gain antennas. I haven't found a simple tool in Linux that could provide a graphical feedback of signal level so I set to make my own script for this. I just need some direction, not asking anyone to write this for me.
For illustration the on-screen feedback would look something like this:
"MyWireless" 00:21:1E:41:E9:50 Channel:6 Signal Level -59 on
===================================================================
"Hotspot" 00:21:1E:41:E9:40 Channel:6 Signal Level -70 off
========================
...
The idea came from Wavemon except this one would show all non-associated APs.
I figured out how to make an animated bar and how to parse the output of 'iwlist scanning' and get the numerical values I need.
I am having trouble separating the iwlist output's cells so I can feed one number at a time to the part of the script that's doing the graphical bar feedback.
will return Signal Level values for all ESSIDs available at the given moment. But I need to filter them by the Address value so I can send only one number to be echoed as an animated bar (or histogram).
I haven't studied in detail how 'iwlist scanning' sorts out the cell output. They are not sorted by signal quality, level, etc. I am guessing that the order of cells may vary so simply filtering the output of the above code based on the order may not work. I think the solution I need has to be contained within the original awk line and tied to a unique identifier like Address.
Any tips are much appreciatted. What an amazing flexibility in Linux. I wish I had more time to play.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.