The State of the Awk
lwn.net
lwn.net
The book inspired me to build a little course on Awk to (hopefully) share the love: https://github.com/FreedomBen/awk-hack-the-planet.
I gave that course as a talk at Linux Fest Northwest in 2019 and then again in 2020 due to popularity. When Covid hit I recorded videos and put them on Youtube. There are links in the Github page above if you want to watch them.
I wasn't intending to self plug, but I guess it does feel relevant :-)
Thanks!
"It was one of a number of text-string-oriented languages developed during the 1950s and 1960s; others included COMIT and TRAC.
SNOBOL4 stands apart from most programming languages of its era by having patterns as a first-class data type (i.e. a data type whose values can be manipulated in all ways permitted to any other data type in the programming language) and by providing operators for pattern concatenation and alternation. SNOBOL4 patterns are a type of object and admit various manipulations, much like later object-oriented languages such as JavaScript whose patterns are known as regular expressions. In addition SNOBOL4 strings generated during execution can be treated as programs and either interpreted or compiled and executed (as in the eval function of other languages).
SNOBOL4 was quite widely taught in larger US universities in the late 1960s and early 1970s and was widely used in the 1970s and 1980s as a text manipulation language in the humanities. ...
SNOBOL4 supports a number of built-in data types, such as integers and limited precision real numbers, strings, patterns, arrays, and tables (associative arrays), and also allows the programmer to define additional data types and new functions. SNOBOL4's programmer-defined data type facility was advanced at the time—it is similar to the records of the earlier COBOL and the later Pascal programming languages.
All SNOBOL command lines are of the form
label subject pattern = object : transfer
Each of the five elements is optional. In general, the subject is matched against the pattern. If the object is present, any matched portion is replaced by the object via rules for replacement. The transfer can be an absolute branch or a conditional branch dependent upon the success or failure of the subject evaluation, the pattern evaluation, the pattern match, the object evaluation or the final assignment. It can also be a transfer to code created and compiled by the program itself during a run.A SNOBOL pattern can be very simple or extremely complex. A simple pattern is just a text string (e.g. "ABCD"), but a complex pattern may be a large structure describing, for example, the complete grammar of a computer language. It is possible to implement a language interpreter in SNOBOL almost directly from a Backus–Naur form expression of it, with few changes. Creating a macro assembler and an interpreter for a completely theoretical piece of hardware could take as little as a few hundred lines, with a new instruction being added with a single line."
I was on a Go spree when I read this book, and after the first few chapters, I thought, "it'd be fun to write a version of AWK in Go". I ended up with https://github.com/benhoyt/goawk
And OP, thanks for posting. I'm the author of the LWN article. Nice to see it re-posted here some six months later. :-)
AWK can be great for simple statistics. I wrote the "num command" tool entirely in gawk, to handle typical math for averages, modes, deviations, kurtosis, etc. It's handy for use on systems where a user can't easily install R/Julia/NumPy/etc.
That's pretty cool! It's really packed with features.
Nice, is it just me/my browser or is http://www.numcommand.com/ down? I was hoping to see if it’s packaged for my distro.
It was using an old bash script(!), to generate the file. I was able learn little awk, and was able to write a 100 line awk script to replicate the same. Apart from the efficiency of the script, which was the main aim, it was impressive how readable and understandable the whole thing became. Some subtle bugs became apparent.
It is not do-all language. Yet, in its small niche, it is very succinct in expressing the text processing ideas. I know it could be done easily in any other language.
A port of bash to awk script : https://github.com/berry-thawson/diff2html/blob/master/diff2...
https://github.com/berry-thawson/diff2html
Original bash counterpart: https://web.archive.org/web/20180612205114/https://github.co...
Edit: Changing link and added new links
The other 10x? That also matches my experience with other people’s python.
Someone probably tried to implement an abstraction, or something foolish like that.
If globals ever do cause a noticeable slowdown due to dynamic lookup, this can easily be fixed by pointing a local var at the global at the start of a function.
You don’t define a function called main, then manually invoke it at the bottom of your bash, perl, etc... script. In python, you do, partially because there is a severe performance penalty for not doing that.
The author explains the idea: “ The program keeps a running sum that it clears when it sees a blank line (NF==0). Other calculations are performed in place. The first occurrence of a variable name defines it as that sum. Subsequent occurrences become the stored value.”
The regexp that defines what the variable name is is simply: /^[A-Z]+[A-Z0-9]*$/ and numbers to sum and variables are always only the first word in the line.
Replacing awk, grep, cut and sort with direct implementations of relational algebra has been on my TODO list for about 10 years.
(V2 would build relational calculus on that)
The one awk script I will never forget, ~30 years ago, was written in 20 minutes when 4 days of C code became untenable.
It was a "hmm.. wonder if I can just push the parsing to AWK" moments that became an epiphany. We didn't have to tell anyone we'd replaced their custom C database loader with a shell script, at least not in those days .. our Friday nights were precious, then.
At work a colleague of mine was looking for some nice way to get some summary of configured/created resources from a kubectly apply with dry run.
I stepped in and provided an awk one-liner that counts and collects the configured and created resources, suppressing the various unchanged one...
Suddenly we were able to clearly see what our kubectl apply were going to touch :)
These days I use AWK on a regular basis for small everyday - tasks.
I wrote a quick and dirty HCL parser in about an hour using functional programming in Perl, and I hadn't really touched Perl in years. It's still pretty nifty.
For more complicated stuff I usually resort to python or even a compiled language.
It’s strength comes from attacking non-tabular data.
I churn through hundreds of gigs a day.