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