I have a file where numbers are continuously appended:
1
2
3
4
I want to calculate their mean, also continuously, i.e.:
1
1.5
2
2,5
I don’t want to check file periodically, I want to it in the manner tail -f work – as soon as a line is appended, I perform mean calculations.
Is it possible?
PS Tried tail -f file.txt | awk '{total+=$0;count+=1;print total/count}' but it hangs without output
You are going to run into buffering issues. Perhaps a solution that will work for you is:
perl -wne 'BEGIN{ $| = 1 } $t += $_; print $t / $. . "\n"; 'The $| = 1 turns off buffering. The rest is the same as your awk script.