162 points · by gregable · 16y ago · 62 comments
Years later I ran into Brian Kernighan at a conference and told him the story, ending it with "and that's when I knew she was the woman for me." He looked at me like I was nuts.
This is 100% true. A coworker of mine implemented an elevation-bitmap-to-3d-model conversion tool in 160 lines of Awk. It ran faster than our "good" Matlab tool by a factor of 10.
Awk (or Perl) doubles the usefulness of Unix. Most of the common commands in Unix are query commands. When you need to start manipulating queried data, Awk is where the rubber meets the road. Piping data through the shell stops being read-only, and becomes interactive.
Could you give a bit more details there? I don't have any experience with matlab, but I tend to think of awk as fast to write code in (and start up), though not particularly fast in execution. (Roughly on par with Python, i.e., usually good enough.)
Still, the One True Awk still has my favorite opening line in its "b.c" source file:
/* lasciate ogne speranza, voi ch'intrate. */Interestingly enough, windows powershell structures its cmdlets in the same way. Makes lots of sense for stream processing as you said.
If the unit of input in this kind of stream processing system doesn't match the problem domain exactly, things get very difficult very quickly.
Regular expressions are my favourite secret weapon; So many problems are made simple by regular expressions and so few people (outside of IT) know of them.
I was curious enough that I bought and read it just at the end of summer. It really is excellent. Highly, highly recommended.
Ieursalimschy's _Programming in Lua_ ("PiL") was written in a similar style. I recommend it quite highly, too. Great language, great programming book.
Also, the PSD, SMM, and USD books (_4.4BSD Programmer's Supplementary Documents_, etc.) are dry, but also have excellent introductions to several classic Unix tools. They're included as documentation in some BSD installations, and should be easy to find otherwise. The intros to lex and yacc are particularly good.
It's incredibly handy, yet the language is small enough that you can learn most of it in an evening, with just a bit longer if you don't know regular expressions.
I do tend to use Perl for things where speed matters, though, especially with large amounts of data going through a regex--- Perl's regex engine seems considerably faster than any awk (or especially sed) I've tested, at least on a few examples I've ported in the past. I was surprised once to get an 8x speedup by porting a 3-line sed script to a 3-line perl script (it was basically doing s/ABC/A\nC/g on a multigigabyte file). I've heard mawk can be speed-competitive with Perl, though.
It's based on PEGs, a different formalism than regular expressions. PEGs are more expressive - they're able to handle balanced, recursive structures, for example. LPEG is a nice middle ground between regular expressions and a full LALR(1) parser.
awk "{print $0}"
does not work. Awk programs need single quotes to prevent bash expansion.
awk 1I can recommend this text file of awk one-liners:
http://www.pement.org/awk/awk1line.txt
And for completeness, here's one for sed:
O yeah and let's not forget Perl.
In other words, awk is unbeatable for stream crunching. (That's the point of being domain specific, by the way.)
I can say the same for ruby and python (and perl).
From personal experience, as an awk script/program becomes more important - it will evolve with more requirements and it will start to be clunky. It just isn't practical to stick with it since you'll eventually need the features/libraries that the other languages have. Given the choices we have today, why even start with awk?
On the performance side, you can always just use Lua if that's really important.
I write a lot of little awk scripts, but if they grow past ~5 lines, they usually get rewritten in Lua. (Perhaps eventually with inner loops in C.) Still, Awk is simple and useful enough that it's still worth knowing.
Again, my point is that you can do the same thing in Ruby, Python, Perl, or Lua just as easily and fast as you can in Awk. Awk used to have a nice niche years back. It pretty much lost that niche the second Perl got popular. It's even more irrevant now that Ruby and Python are even easier and faster to build stuff with quickly
About the only real pragmatic reason I can think of for learning Awk is to migrate existing Awk scripts, that started as nice useful one liners but eventually evolved into spaghetti, to Python / Ruby
| awk '{print $2}' | ...
| python -c 'import sys
for line in sys.stdin:
try:
print line.split()[1]
except IndexError:
print' | ...http://zork.net/~nick/loyhargil/if/if.awk
For comparison, here are all the published examples of this exercise in a variety of systems:
http://www.firthworks.com/roger/cloak/
I won't say it's the best tool for this job, but I feel that the awkishness provides a certain elegance to some aspects.
I'd bet that people get shit done 10x+ times faster in awk/lua/python/ruby/lisp/whatever until having to work with nasty C++-specific libraries dominates, though. (C is friendlier that way.)