The Psychological Benefit of AWK

· jollygood's blog

#keepitsimple

Some years ago, I had to parse a lot of log files while troubleshooting an issue on some servers. I didn't have any fancy tools at hand, so my first approach was to filter the logs with grep and pipe the output into vim to do manual inspection. Remove non-relevant data, search for specific patterns. After doing this for a while, a pattern began to emerge. My usual approach at this stage would be to write a script or a little program to automate the stuff I did manually with grep and vim but (un)fortunately none of my go-to programming languages were available on these servers. AWK to the rescue!

AWK is a small script language that's been around since 1977. This language is ideal for minimal scripts to parse textual data, usually a one-off script. I first heard about AWK in university many years ago and had occasionally played around with it in my free time. But I had never used it at work, since most of my career until then had been in Windows based environments. I had some UNIX/Linux/BSD knowledge from my studies and side-projects, but at the time for this story things were changing. Lucky me, I get to work with Linux as part of my day job nowadays!

Anyway, I caught up on AWK and got the job done. I was amazed of how powerful AWK was! I could do a lot with a few lines, stuff that otherwise would take me tens of lines of code in a "real" programming language. All right, I could probably do it in less than ten lines of Python but that script would be ugly and horrible to maintain. (This is an observation of compact scripts, not of any specific programming language).

I usually want to put some effort into my scripts to make them readable and maintainable. "Real" programming languages provide the opportunity to do fancy stuff, and there is the temptation to over-engineer a small script into a unnecessarily large one. I think here lies a psychological benefit of AWK, at least for me: with AWK, I'm not that tempted to over-engineer my script. It's good enough with a one-liner or 2-3 lines. In fact, AWK was designed for one-liners and is not suitable for larger programs, hence the risk of over-engineering an AWK script is significantly lower.

It seems AWK was designed to help the programmer follow the KISS principle. I like that.