问题
I'm looking for caveats, tips'n'tricks etc. for awk. For example:
awk '$9=="404"{a[$7]++}END{for(i in a)print a[i],i}' access.log|less
this code, will print errors aggregated by page path.
There is a trick, to sort an array by setting WHINY_USERS to any, nonzero value, to automatically use isort function on array before printing:
WHINY_USERS=1 awk '$9=="404"{a[$7]++}END{for(i in a)print a[i],i}' access.log|less
This code will return same errors but sorted by the key name (path).
I'm looking for more tricks like this one - do you know any resource which has them listed? could you share best tricks you know? I've never found awk wiki - only same old tuts repeated over and over...
回答1:
Does Awk One Liners count?
http://www.pement.org/awk/awk1line.txt
回答2:
In version 4.0 gawk
got a debugger.
http://www.gnu.org/software/gawk/manual/html_node/Debugger.html
Starting dgawk is exactly like running awk. The file(s) containing
the program and any supporting code are given on the command line
as arguments to one or more -f options. (dgawk is not designed to
debug command-line programs, only programs contained in files.) In
our case, we call dgawk like this:
$ dgawk -f getopt.awk -f join.awk -f uniq.awk inputfile
来源:https://stackoverflow.com/questions/11697556/tipsntricks-in-awk