Powered by Blogger.

Thursday, October 23, 2008

Tag: , , , ,

AWK. Calculate mean, min and max

AWK is a general purpose programing language that is designed for processing text-based data. This unix command is very powerful and with it we can calculate the mean, min and max of a data stored in file.

data.txt:
10
20
50
100
200
500

awk command:
awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1} END {print total/count, min, max}' data.txt

Result:
146.667 10 500

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.

21 comments:

  1. A small gift for you. This awk program is an extension of yours that accept different column numbers and also print the line index where min and max values occur.

    #get-mean-min-max.awk
    #You must define COL with option -v of awk
    {
    if(min=="")
    {
    min=max=$COL
    };

    if($COL>max)
    {
    max=$COL
    maxIdx=NR
    };

    if($COL< min)
    {
    min=$COL
    minIdx=NR
    };

    total+=$COL;
    count+=1
    }

    END {print total/count "\n" min " - " minIdx "\n" max " - " maxIdx}

    ReplyDelete
  2. awk ‘{if(min==”")min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1} END {print total/count, min, max}’ data.txt

    ReplyDelete
  3. I had been looking for this function for ages, and this is it (and even more than I was looking for)!! thanks

    ReplyDelete
  4. [...] Solution I made with the help of these guides 1 and 2 is given below. Save the lines between the ###### as a file, say meanminmax. [...]

    ReplyDelete
  5. [...] Solution I made with the help of these guides 1 and 2 is given below. Save the lines between the ###### as a file, say meanminmax. [...]

    ReplyDelete
  6. Amazing blog:) I am going to want a bit of time to toy with this article!

    ReplyDelete
  7. The single quotes around the script cause a "bash: syntax error near unexpected token '(' " to occur when dragging and dropping the script on some computers. This is easily fixed by deleting and replacing them with single quotes from your keyboard. The first double quote causes another error, "invalid character ..." to appear just before the END block. Fix it the same way, replace the first " with one from your keyboard.

    Total/Count is the Average but not the statistical mean.

    ReplyDelete
  8. OMG .. i don't have other words :)

    ReplyDelete
  9. Nice, clean , simple , see below:

    awk -F ';' ‘{if(min==”"){min=max=$2}; if($2>max) {max=$2}; if($2< min) {min=$2}; total+=$2; count+=1} END {print total/count, min, max}’ data.txt

    ReplyDelete
  10. I believe that is one of the such a lot important info for me. And i'm satisfied reading your article. However wanna statement on few general issues, The site taste is perfect, the articles is in reality great : D. Just right activity, cheers

    ReplyDelete
  11. never like testing uninitialized variables. avoid by using awk's NR:


    awk ‘{if(NR==1)min=max=$1; if($1>max) max=$1; if($1< min) min=$1; total+=$1; count+=1} END {print total/count, min, max}’ data.txt

    ReplyDelete
  12. It is very simple command to find the min and max values. Thanks for such a beautiful awk command.

    ReplyDelete
  13. good information shared.. helped me a lot to complete my job easily.. cheers to you guys...

    ReplyDelete
  14. Aw, this was an incredibly good post. Finding the time and
    actual effort to generate a superb article… but what
    can I say… I put things off a whole lot and never manage to get nearly anything done.

    ReplyDelete
  15. it is very useful, I am working on a project for univ ouargla thanks

    ReplyDelete
  16. Wonderful post! We are linking to this particularly great content on our website.
    Keep up the good writing.

    ReplyDelete