Awk by Example
developer.ibm.com
developer.ibm.com
Yesterday, I wrote production level code using awk. It was either write the code in go which I figured would take all day. But I had really about an hour or so. 40 lines of shell script mixture of about 20 lines of awk and bash/sed/grep/tail/xargs/cut/cat/curl got the job done.
AWK is very freaking powerful if you learn to parse data with it.
[0] https://en.wikipedia.org/wiki/Alfred_Aho
[1] https://en.wikipedia.org/wiki/Peter_J%2E_Weinberger
[2] https://en.wikipedia.org/wiki/Brian_Kernighan
[3] https://en.wikipedia.org/wiki/The_AWK_Programming_Language
Also, the gawk book is written by Arnold Robbins who is one of current maintainers of the tool and as per [1] gawk was started by David Trueman and Arnold.
[1] https://www.gnu.org/software/gawk/manual/html_node/Foreword3...
I really enjoyed this article, because now that I have a sampling of the class of problems that awk can help me with, I am more likely to use it. And it only took 10 minutes to get this overview. My brain is already buzzing with ideas on how I can use this with gron[1] that was mentioned a few days ago on HN.
We all need a compelling reason for a tool before investing the time for whatever Bible may exist, so "just grab" the Bible first instead isn't the most helpful advice :) But I do appreciate you flagging it!
[1] https://github.com/tomnomnom/gron
---
P.S. For those who liked the writing style of this tutorial, you may enjoy these other ones on the IBM website by the same author Daniel Robbins: https://www.ibm.com/developerworks/views/linux/libraryview.j...
PATTERN { ACTION }
statements, where { ACTION } is optional and defaults to { print }Side note; I almost always use Perl or Ruby with the -n or -p flags instead of awk. I don't need to have three or four syntaxes to remember when I can use just one.
There's more to that:
Either PATTERN or { ACTION } is optional (but not both). If no PATTERN is given, the default means "match every line" (and so do the given ACTION for every line).
The ACTION could be anything (even an explicit print, whether or not PATTERN is given), summing up some column, filtering more on substring(...), assigning values to variables, comparing variables and doing actions based on them (awk's if statement), calling a built-in or user-defined function, etc.
More people should check prior art before making new stuff. Reuse / stand on the shoulders of giants / don't reinvent the wheel, etc. But NIH is alive and well ...
Technically??