This week I read the AWK Programming Language by Alfred Aho, Peter Weinberger, and Brian Kernighan. A long time ago I learned the basics of AWK, just enough to read my way through some basic scripts. But I never took the time until this week to sit down and completely wrap my head around this programming language from 1977, and to see why it still has merit and uses decades later.
Jumping Right In
AWK is a data-driven programming language that works with text files. The first chapter of the book opens with a whirlwind tutorial that demonstrates enough of AWK that some readers will be able to start using the language then-and-there. The text coaxes the reader into typing out the examples, which in my opinion helps crystallize the information in one’s head. Most of the examples in the first chapter are one-liner programs—it even ends with a list of them—but it also shows that AWK programs can be more than a short command at a Unix shell.
Chapter Two explains the AWK language in full detail. The authors are terse but efficient, making the book a dense chunk of information. The size of the book, less than two-hundred pages of content, is kept at bay by both the simplicity of AWK and the authors’ straight-forward language.
AWK programs receive input one line at a time, check for any conditions that match that line, and if there are any then it runs the commands associated with those conditions. That makes AWK a useful middleman to sit between two sources of data, converting and translating as it passes through. Chapters Three through Seven demonstrate a variety of ways to do exactly this. The book shows its age here as it seems to assume that all data is in nice text formats; it is a nice Unix philosophy, but it does not match reality. I do not remember if the book out-right says you cannot use AWK to process binary files, but these chapters make it clear that AWK is not meant for that task even if you could somehow force it.
Chapter Seven is particularly interesting, titled ‘Experiments With Algorithms’. It explains some common sorting algorithms, using them as examples of how AWK can function as not-really-pseudo pseudo-code. Someone unfamiliar with how insertion sort works, for example, should have an easy time understanding the explanation from the book. AWK indeed makes for some nice, readable pseudo-code that happens to be in a usable programming language, unlike pseudo-code you see in many algorithm texts. The chapter only talks about sorting algorithms but it still manages to impress. It is well-written to the point that it forces me to consider AWK as a useful aid for teaching computer programming.
Most chapters end with exercises for the reader. They vary in difficulty, but the answers are available from an appendix. I suggest typing out the examples through each chapter as you read them; by the time you reach the exercises you’ll have the concepts firmly implanted and should be able to answer them readily.
Next Book of the Week
The AWK Programming Language was fun to read and I’m glad that I now have a better understanding of the language. I can see myself using it for simple scripts where I want a little more ease-of-functionality than a shell script offers, but I also do not want to pull out something like Python. If you want more information than check out the AWK Community Portal.
Next week I am reading Hacking Secret Ciphers With Python, a book you can download for free. So consider reading along!