Interesting uses of awk command.

If you are new to the awk command, then I would suggest you read the article "A very quick introduction to AWK". I love using the awk command to find stats from log files. I would like to share few of these interesting commands which has helped me finding some stats.

Test log file to process

I am taking following input from a log file named file.log for my example.

Completed processed item 1 (1.23s).
Completed processed item 2 (0.5s).
Completed processed item 3 (0.22s).
Completed processed item 4 (0.18s).
Completed processed item 5 (0.75s).
Completed processed item 6 (12.31s).
Completed processed item 7 (0.85s).
Completed processed item 8 (0.25s).

Finding sum of values or total time etc.

You could run this command to find the total time taken to process.

grep -E -o "\(([0-9\.]+)s\)" file.log | grep -E -o "[0-9\.]+" | awk '{ sum += $1 } END { print sum  }'

Finding average of values or average time etc.

You could run this command to find the average time taken to process.

grep -E -o "\(([0-9\.]+)s\)" file.log | grep -E -o "[0-9\.]+" | awk '{ sum += $1 } END { print sum / NR }'

Finding minimum and maximum of values or average time etc.

You could run this command to find the minimum time taken to process.

grep -E -o "\(([0-9\.]+)s\)" file.log | grep -E -o "[0-9\.]+" | awk '{ if (min > $1 || length(min) == 0) min = $1 } END { print min }'

You could run this command to find the maximum time taken to process.

grep -E -o "\(([0-9\.]+)s\)" file.log | grep -E -o "[0-9\.]+" | awk '{ if (max < $1) max = $1 } END { print max }'