Saturday, February 27, 2010

How many occurrences since November the 1st last year?

AWK is just great! It's just amazing how powerful one-liners you can craft.

But since I never really need it - as in "I couldn't have done this/my job without it", I seldom use it, and tend to forget the most useful invocations I find.

So... here's a useful one-liner I'm sure I'll need to look up again sometime.

myhost $ awk '{op[$6]++} END {for (o in op) print o, op[o] }' access.log

  • access.log here is a server-side file logging each invocation of some web service methods. (not that you should care :) The file has a rigid structure: One line per invocation containing method name, caller, timestamp.
  • op[$6]++ Constructs a hash table named op with method names as keys and numbers of invocations as values. (Default field separator is whitespace.)
  • When the file has been processed, the results are printed out
Throwing in a date is easy, and can be useful:

myhost $ awk '{$1 > "2009-10-31" && op[$6]++} END {for (o in op) print o, op[o] }' access.log

No comments:

Post a Comment