Yes. Pipe tail -f
to a script that handles your averaging. The pipe will never close and the script can process instantly for each line it receives... it will block until a line appears.
Also, one should keep in mind that it is possible to calculate a running average without having to add all the values each time. I've seen it that enough I feel the need to mention it.
#generator.pl
$| = 1; #immediate flush
while (1) {
print int rand(100), "\n";
sleep 1;
}
#average.pl
$| = 1; #immediate output flush
my $average = 0;
my $count = 0;
while (<>) {
$average = ($average * $count + $_) / ($count + 1);
$count++;
print $average, "\n";
}
$ perl generator.pl > source &
[2] 15564
(reverse-i-search)`': ^C
$ tail -f source | perl average.pl
54
28
27.6666666666667
35
41
And, just for grins:
$tail -f source | awk '{total+=$0; count+=1; print total/count}'
That also has instant feedback. It seems to me that your issue is buffering by the application that is writing to the file that tail is reading from.
See http://www.pixelbeat.org/programming/stdio_buffering/ for info on that.