Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash command which gives me the highest value in the 9th column of a file:

cat "log.txt" | grep 1923 | awk '{print $9}' | sort -n | tail -1

log.txt is a log of CPU usage for process 1923 & is measured 10 times per second.

I need to verify that process 1923 never exceeds a certain threshold for more than one second.

Could someone please help me formulate an awk command that:

  1. Calculates a rolling 1-second average
  2. Tells me the peak value it finds

Many thanks, Fidel

share|improve this question
I'm confused as to how a one-liner (or grawity's magnificent awk script, for that matter) could produce a "rolling average" if they are manually run against a log ... you'd need to tail the log for that, and an awk script w/ a END clause wouldn't work for printing peak values out. – tink Mar 21 '13 at 21:26
Hi tink, the awk script is being run on a file that's not being updated constantly...does that answer your question? – Fidel Mar 21 '13 at 22:18
Heh. Kind of boils down to semantics, then. I don't think 'rolling' is a good word in the given situation. :) – tink Mar 21 '13 at 22:20
Have you considered using ulimit to prevent the command from exceeding a threshold? – chepner Mar 21 '13 at 22:52
hah, I get you now tinks ;) – Fidel Mar 22 '13 at 7:50
up vote 3 down vote accepted
#!/usr/bin/awk -f
# NOTE: This example takes only CPU load as input. Use it like this:
#    cat log.txt | awk '$1 == "1923" {print $9}' | awk -f rolling-average.awk

    freq = 10; sum = 0; max = 0

    i = NR % freq
    if (NR > freq)
        sum -= data[i]
    sum += (data[i] = $1)

NR >= freq {
    avg = sum/freq
    print "average for " NR-freq+1 ".." NR " = " avg
    if (avg > max) {
        max = avg
        pos = NR

    print "peak " max " at " pos-freq+1 ".." pos

Edit: Fixed a possible bug and removed the line filtering (which made things more complex than necessary) – now it needs to be given CPU load numbers only. But at least it calculates the average correctly now.

share|improve this answer
wow grawity, I'm impressed! Thank you very much! – Fidel Mar 24 '13 at 13:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.