Part of my datafile looks as
ifile.txt
1
1
3
0
6
3
0
3
3
5
I would like to find probability of each numbers excluding zeros. e.g. P(1)=2/8; P(3)=4/8 and so on
Desire output
ofile.txt
1 0.250
3 0.500
5 0.125
6 0.125
Where 1st column shows the unique numbers except 0 and 2nd column shows the probability. I was trying as following, but looks very lengthy idea. I am facing problem in for loop, as there are so many unique numbers
n=$(awk '$1 > 0 {print $0}' ifile.txt | wc -l)
for i in 1 3 5 6 .....
do
n1=$(awk '$1 == $i {print $0}' ifile.txt | wc -l)
p=$(echo $n1/$n | bc -l)
printf "%d %.3f\n" "$i $p" >> ofile.txt
done