Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 series piped greps, awks and seds which produce a list of numbers, one on each line. Something like this:


How can i pipe this to something which will output the average, max, and min?

share|improve this question
If you're piping grep, awk and sed together, the same thing can often be done in one invocation of awk. – Dennis Williamson Feb 23 '11 at 23:54
up vote 22 down vote accepted

Since you're already using awk

blahblahblah | awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1<min) {min=$1}; total+=$1; count+=1} END {print total/count, max, min}'
share|improve this answer
Nice, it works for me! – JavaRocky Feb 23 '11 at 23:44
I thought there would of been already a binary which accepts a list of numbers and the count, avg, min, max for you, kind of like using 'time'. – JavaRocky Feb 23 '11 at 23:45
I would just put the above awk pattern in /usr/local/bin/stats or such, and then use it as blabla | stats. – A-B-B Aug 29 '13 at 20:11

I find this program useful for generating stats on lists of numbers at the command line:

share|improve this answer

There is also simple-r, which can do almost everything that R can, but with less keystrokes:

To calculate average, max, and min, one would have to type one of:

r summary file.txt
r summary - < file.txt
cat file.txt | r summary -
share|improve this answer

With a tip of the hat to @DerfK:

perl -lane '$n=$F[0]; if(not defined $min){$min=$max=$n}; if($n>$max){$max=$n}; if($n<$min){$min=$n}; $total+=$n; $count+=1; END{print $total/$count." $max $min"}'

$F[0] is the value in the first (0'th) field of each line

If your input data is comma separated, add the -F, modifier before -lane

share|improve this answer

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.