4 ms·
If you know C and regex, that's 85% to 95% of awk. The basic layout of an awk program is a sequence of regex/action pairs, where the regexes are between slashe
by smcameron 5y ago
If you know C and regex, that's 85% to 95% of awk. The basic layout of an awk program is a sequence of regex/action pairs, where the regexes are between slashes, and the actions are between curly braces. (There's also the special regex values of BEGIN and END, executed prior to the first line of input and after all input is consumed, respectively). The action part has a syntax very much like C, with the special values $n (where n is an integer) representing field number n for a any input line. There are a bunch of special functions it defines that you can call, like substr(), or split(), and good old printf. Awk processes the input stream one line at a time, matching each line against the series of regexes, and if it matches a regex, it executes the action paired with that regex, with all the $n variables filled in with field values from that input line. And that's basically enough for 99% of what awk is used for on a day to day basis.