View Single Post
Old 21st March 2009
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 1,027
Default

awk is great; the syntax is a lot like C, only less finicky about declarations. So if you know C you can get started quickly (and perhaps vice versa).

But long ago in my first brushes with awk, I was very confused and bogged down in the command-line syntax, patterns and pre-defined variables. The big picture was missing, and it really didn't start to click until I realized a simple analogy that made it clear.

So here's my mini-contribution to awk 101 . (For those who know awk, allow me the leniency of over-simplification in descrbing this analogy.) In a language like C, the functions have names. The code within the function block gets executed when the function is called by name, either from another such function or from main().

The analogy is that awk is like this, except the "functions" don't have a name: instead they have a pattern associated with them. The code in a "function" block gets executed when the pattern matches (part of) an input-data line.

To me, that's awk in a nutshell, the rest is details. (Of course, the "functions" are called "action statements" and awk does have named functions of its own just like in C.)

Happy awking!

Last edited by IdOp; 21st March 2009 at 06:42 PM.
Reply With Quote