Just started, the aim of this page is to collect small pieces of code that illustrate specific problems.
Fields are numbered from 1 to NF, so you can easily use a for loop, and the derefence operator, for instance to print each field matching foo:
for (i=1;i<=NF;i++) { if ($i ~ /foo/) { print $i } }
You can test if an array is empty using this function:
function empty(a, i) { for(i in a) return 0; return 1}
This function comes from comp.lang.awk see this post
The function 'match' sets RSTART to the index of each match and RLENGTH to its length, and returns 0 when no matches exist. To find the last match on a line, then, all that is necessary is to call it and remove the match until it no longer finds matches, and take the final match.
function lastmatch(re, line, result) { while(match(line, re)) { result = substr(line, RSTART, RLENGTH) line = substr(line, RSTART+RLENGTH) } return result }