Help with Median

Hi,

I know this question has been asked before, but I'd like to ask for some explanation rather than solution regarding finding median in unix.
I've got a simple one column file with over 2 million numbers and I need to find its median. The file looks like this:
0.123
0.235
0.890
0.000 etc (just plain numbers).

The solution that I've found in forum answers was:
1) find the number of lines with this code:

wc -l filename

In my case it is: 2543887

2) if the number is not even (like in my case) than the median can be found with this code:

cat filename| head -$2543887 | tail -1

My question is how head -2543887 is different to head -$2543887? What does the dollar sign do?

Many thanks in advance!

cat filename| head -$2543887 | tail -1

Is it a correct command? Did you try this?? and cant see any maths here to get the median. am confused.

Yes, I've tried it and the answer was 0.0751, while typing just
cat filename | head -$254887 | tail -1 gives 9.9997. So, these two command lines do something different?

I'm new to Unix. Do you think what I found won't give me the median? What do you think I should use?

Many thanks!

---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ----------

sorry, I meant that typing
cat filename | head -2543887 |tail -1 gives 9.9997
So, typing it with or without the dollar sign gives different outputs...

i did not knew this $ thing with head...
and for median, i think you need to do the statistcs part. I dont know the formula :slight_smile:

I don't follow, how the head|tail can calculate the median.

Unofrtunately, I'm new to Unix and don't know how the head and tail can calculate the median, but I thought that the dollar sign is a trick. This is why I asked the question in the first place.
Does anyone know how to calculate the mean, then?

---------- Post updated at 08:14 AM ---------- Previous update was at 08:10 AM ----------

sorry, I meant how to calculate the median

Google is your friend

Thank you Vgersh99, but I'm afraid it's a bit too complicated for me. Being a beginer, I don't get half of what is written there... Sorry!

[Courtesy of Awk]

nawk -f stats.awk myFile

stats.awk:

# $Id: moments.gawk,v 1.1 2001/05/13 07:09:06 doug Exp $
# http://www.bagley.org/~doug/shootout/

BEGIN {
    #delete ARGV;
    sum = 0;
    n = 0;
}

{
    nums[n++] = $1;
    sum += $1;
}

END {
    mean = sum/n;
    for (num in nums) {
    dev = nums[num] - mean;
    if (dev > 0) { avg_dev += dev; } else { avg_dev -= dev; }
    vari += dev^2;
    skew += dev^3;
    kurt += dev^4;
    }
    avg_dev /= n;
    vari /= (n - 1);
    std_dev = sqrt(vari);

    if (vari > 0) {
    skew /= (n * vari * std_dev);
    kurt = kurt/(n * vari * vari) - 3.0;
    }

    nums[n] = nums[0];
    heapsort(n, nums);

    mid = int(n/2)+1;
    median = (n % 2) ? nums[mid] : (nums[mid] + nums[mid-1])/2;

    printf("n:                  %d\n", n);
    printf("median:             %f\n", median);
    printf("mean:               %f\n", mean);
    printf("average_deviation:  %f\n", avg_dev);
    printf("standard_deviation: %f\n", std_dev);
    printf("variance:           %f\n", vari);
    printf("skew:               %f\n", skew);
    printf("kurtosis:           %f\n", kurt);
}

function heapsort (n, ra) {
    l = int(0.5+n/2) + 1
    ir = n;
    for (;;) {
        if (l > 1) {
            rra = ra[--l];
        } else {
            rra = ra[ir];
            ra[ir] = ra[1];
            if (--ir == 1) {
                ra[1] = rra;
                return;
            }
        }
        i = l;
        j = l * 2;
        while (j <= ir) {
            if (j < ir && ra[j] < ra[j+1]) { ++j; }
            if (rra < ra[j]) {
                ra = ra[j];
                j += (i = j);
            } else {
                j = ir + 1;
            }
        }
        ra = rra;
    }
}

Yes, thank you! I have seen it through the link you've attached, but I don't understand it. I will look thnigs up, but was hoping there's a simpler way of doing it. Somebody's hinted that I should use awk, but then I saw something on this forum and decided to ask.
Does anybody know anything simpler than this huge script, please?

Many thanks in advance!

---------- Post updated at 09:28 AM ---------- Previous update was at 09:24 AM ----------

I have just tried ot run
nawk -f stats.awk myfile
but it doesn't work. Nawk is not recognized as a command...

sort myDataFile | awk -f median.awk

median.awk:

{
    nums[++n] = $1;
}

END {

    mid = int(n/2)+1;
    median = (n % 2) ? nums[mid] : (nums[mid] + nums[mid-1])/2;

    printf("median:             %f\n", median);
}

---------- Post updated at 10:38 AM ---------- Previous update was at 10:37 AM ----------

then use either 'awk' or 'gawk'

It does work now! I've got loads to learn and understand now!!!!

Many thanks for your help!!!!