We can use the dc
utility to perform the calculations :
$ < myfile tr -s ' ' '\t' | cut -f1 |
dc -e "
[lM lN / p q]sq
[lM 1 + sM]sa
[? z0=q lN 1 + sN d2!>a c z0=?]s?
4k 0sN l?x
"
Result:
.3333
Brief Explanation:
° Register `N` holds line count.
° Register `M` holds num of lines >= 2.
° Register `q` performs the division, printing it, and quitting. Kinda like the `END` clause of `awk`.
° Register `a` increments the current value stored in register `M`.
° Register `? ` reads the next line from stdin, checks whether it is empty. In case it us then it initiates the end procedure by invoking the q register. Otw, increments register N the one keeping the line count. Then compares the current line is greater than or equal to 2 . Increments reg M if it is. Then calls itself recursively to redo the same set of operations on the next line.
° 4k will set output accuracy to four digits and 0sN shall initialize the line counter, l?x will set the ball rolling recursively.