# Help with Median

**URL:** https://community.unix.com/t/help-with-median/244778
**Category:** UNIX for Dummies Questions & Answers
**Created:** [July 16, 2009, 8:05am UTC](https://community.unix.com/t/help-with-median/244778 "2009-07-16T08:05:39Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 8:05am UTC](https://community.unix.com/t/help-with-median/244778/1 "2009-07-16T08:05:39Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![rakeshawasthi](https://community.unix.com/user_avatar/community.unix.com/rakeshawasthi/32/224_2.png) [@rakeshawasthi](https://community.unix.com/u/rakeshawasthi)
#### Post date: [July 16, 2009, 8:46am UTC](https://community.unix.com/t/help-with-median/244778/2 "2009-07-16T08:46:39Z")

</div>

```nohighlight
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.

---

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 8:59am UTC](https://community.unix.com/t/help-with-median/244778/3 "2009-07-16T08:59:40Z")

</div>

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...

---

<div class="post-metadata">

### Author: ![rakeshawasthi](https://community.unix.com/user_avatar/community.unix.com/rakeshawasthi/32/224_2.png) [@rakeshawasthi](https://community.unix.com/u/rakeshawasthi)
#### Post date: [July 16, 2009, 9:05am UTC](https://community.unix.com/t/help-with-median/244778/4 "2009-07-16T09:05:01Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)
#### Post date: [July 16, 2009, 9:07am UTC](https://community.unix.com/t/help-with-median/244778/5 "2009-07-16T09:07:21Z")

</div>

I don't follow, how the head|tail can calculate the [median](http://en.wikipedia.org/wiki/Median).

---

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 9:14am UTC](https://community.unix.com/t/help-with-median/244778/6 "2009-07-16T09:14:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)
#### Post date: [July 16, 2009, 9:45am UTC](https://community.unix.com/t/help-with-median/244778/7 "2009-07-16T09:45:32Z")

</div>

Google is your [friend](http://dada.perl.it/shootout/moments_allsrc.html)

---

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 10:03am UTC](https://community.unix.com/t/help-with-median/244778/8 "2009-07-16T10:03:44Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)
#### Post date: [July 16, 2009, 10:20am UTC](https://community.unix.com/t/help-with-median/244778/9 "2009-07-16T10:20:00Z")

</div>

[Courtesy of [Awk](http://dada.perl.it/shootout/gawk_allsrc.html)]

nawk -f stats.awk myFile

stats.awk:

```plaintext
# $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;
    }
}

```

---

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 10:28am UTC](https://community.unix.com/t/help-with-median/244778/10 "2009-07-16T10:28:28Z")

</div>

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...

---

<div class="post-metadata">

### Author: ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)
#### Post date: [July 16, 2009, 10:38am UTC](https://community.unix.com/t/help-with-median/244778/11 "2009-07-16T10:38:16Z")

</div>

sort myDataFile | awk -f median.awk

median.awk:

```plaintext
{
    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 ----------

> [@zajtat](#):
>
> 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...

then use either 'awk' or 'gawk'

---

<div class="post-metadata">

### Author: ![zajtat](https://community.unix.com/user_avatar/community.unix.com/zajtat/32/5222_2.png) [@zajtat](https://community.unix.com/u/zajtat)
#### Post date: [July 16, 2009, 10:46am UTC](https://community.unix.com/t/help-with-median/244778/12 "2009-07-16T10:46:04Z")

</div>

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

Many thanks for your help!!!!
