Friday, April 18, 2014

Grabbing the statement durations out of a log file using gnu grep

Sometimes I find myself wanting to grep all the query durations out of a file for sorting.  I recently discovered that I can use gnu grep's lookbehind syntax like so:

grep -oP '(?<=duration: )[0-9\.]+' postgresql-Mon.csv

and then sort them numerically like so:

grep -oP '(?<=duration: )[0-9\.]+' postgresql-Mon.csv | sort -n | tail

1 comment:

  1. The awk version:
    awk '$8 == "duration:" {print $9}' | sort -n | tail

    You can also use awk to find entries that exceed a certain duration, for example:
    awk '$8 == "duration:" && $9 > 1500'

    Depending on your log prefix you may want to substitute $8 and $9 with your column numbers.