An awk idiom: getting fields backwards from the end of the line

July 25, 2006

It's easy in awk to get fields counting from the start of a line; the first field is $1, the second field is $2, and so on. But periodically I'm not interested in fields at the start of the line, I'm interested in a field at the end of the line; it's much easier to see that it's the third-last field than to carefully count how many fields it is from the start.

(And sometimes you have a variable number of fields in a line but you know that what you want is always Nth field from the end, in which case counting up from the front doesn't help at all. One common case is when a logical field can sometimes have whitespace, so awk will turn it into a variable number of fields.)

Fortunately awk has a way out: '$' is actually an operator (a very high precedence one), so it takes expressions as well as just numbers, and the NF variable is the number of fields in the current line.

Because I keep forgetting this: the last field in the line is '$(NF)', and '$(NF-1)' is the second last field. (Because awk counts fields from 1 instead of from 0, unlike Python and Perl.)

(Okay, technically you can use the $[ magical variable to make Perl 1-based, or in fact arbitrarily based. Don't.)

Written on 25 July 2006.
« Reading Unix manpages
xterm's ziconbeep feature »

Page tools: View Source.
Search:
Login: Password:

Last modified: Tue Jul 25 16:21:29 2006
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.