An Introduction to AWK – A Simple Essential Language

CLICK HERE FOR MORE DETAILS on how to utilize Ciscocmd to monitor you entire network at a glance and keep your finger on the pulse of your global communications infrastructure. […]

CLICK HERE FOR MORE DETAILS on how to utilize Ciscocmd to monitor you entire network at a glance and keep your finger on the pulse of your global communications infrastructure.

SUBSCRIBE HERE for to receive useful updates and tips for Network Administration and Automation.

Introduction
The title of this article is a bit misleading. AWK, named after its creators, legendary Bell Labs Engineers Aho, Weinberger, & Kernighan, is actually a fully functioning programming language.

Reference
There are hundreds of great AWK primers available on the web. One good reference is “An AWK Primer” and can be found at:

http://www.vectorsite.net/tsawk.html.

Another good reference is an online tutorial by Bruce Barnett that can be found at:

http://www.grymoire.com/Unix/Awk.html.

Finally, O’Reilly media has a nice book called  “sed & awk, Second Edition” that can be easily found on Amazon or the O’Reilly website.

Briefly for the purposes of this article, we will be using AWK mainly for it’s text and column processing functionality. Here is an example of what AWK can do.

Example: I file contains the following lines:

a b c d
1 2 3 4
a 1 2 3
a 1 4 5

With AWK you can display only the columns that you are interested in:

awk ‘{print $2}’ | grep 2 | wc -l  –> The output would be the number 1

In this case, I printed only the second column, then filtered for something I was looking for, in this case 2, and finally counted the number of instances. The column functionality is very helpful in formatting and processing text output of other network administration automation scripts.

AWK Example
For the purposes of Network administration, AWK is most often used as a simple text processor. I use it most often to process the output of a network admin script. Below are some simple examples of AWK network administration scripts.

Script to Dump CAM table on a switch and print format output to print only

vlan, MAC address, and port numbers.

First we would use the Cisco command utility, which is described in detail on the enetworkadmin.net site called “Cisco Command The Ultimate Little Tool”. You can also access the article at the following LINK.

The ciscocmd utility would allow us to log in to a switch or a series of switches and dump the CAM table. However this would be just a jumble of unformatted data. We could do the following to make the data more useable.

ciscocmd-output | awk ‘{print “Vlan”$1,”MAC-Addr”,$2,”Int”,$6}’

The above line would transform data that looks like this:
12  0001.d82f.ec8a   dynamic  Yes          0   Gi1/3

To this:
Vlan12 Mac-Addr 0001.d82f.ec8a Int Gi1/3

Notice that when I put a “comma” in the awk statement it adds a space in the output. This is a simple example of how you can manipulate text output. You may not think it’s very useful. If you add a few more steps you will start to see that you can build a very useful data table.

Extending the example. So now say you had a hundred switches. You may want to augment the resulting output with the actual switch name. Like this:

Switch1 Vlan12 Mac-Addr 0001.d82f.ec8a Int Gi1/3
Switch2 Vlan14 Mac-Addr 0001.d93f.ec8a Int Gi5/5

Referring to the Cisco Command article on the enetworkadmin.net site at the following LINK , you see that you can pass the switch variable from a shell script, like the following:

——
while read host
do
./ciscocmd -u usr -p passwd -t $host -c “show mac-address-table”
done
——

In order to tag the name onto the output, all you need is the following addition to the line:

./ciscocmd -u usr -p passwd -t $host -c “show mac-address-table” | awk ‘{print “‘”$host”‘”,”Vlan”$1,”MAC-Addr”,$2,”Int”,$6}’

Now you would have a large flat file with lots of useful data. Someone may ask you, what switch contains vlan 12? You can grep (grep is a unix command) for Vlan12 and the switch will appear. Or they may ask, do we have any Vlan ID’s that are replicated across multiple switches. You could do the following command: awk ‘{print $1,$2}’ filename | sort | uniq. The output would be all the unique vlans configured on every switch.

As you can see, there are many simple things you can do with AWK that combined with a few other commands quickly become very powerful tools. In the future, eNetworkAdmin.net will feature articles that apply the information in this article and others for monitoring your wireless networks. Stay tuned.

Thanks for taking the time to read this article. As always please send any feedback you have to info@enetworkadmin.net. We would love to hear from you.

CLICK HERE FOR MORE DETAILS on how to utilize Ciscocmd to monitor you entire network at a glance and keep your finger on the pulse of your global communications infrastructure.

SUBSCRIBE HERE for to receive useful updates and tips for Network Administration and Automation.

About berkel8