1
1
1nd1go2012-02-22 19:38:57
bash
1nd1go, 2012-02-22 19:38:57

Shell script question: continuous tail -f processing?

Greetings!
I want something strange...
Let's say I have numbers falling into a certain file:
1
2
3
4
I want to continuously, as new lines arrive, calculate their average. Those. to display
1
1.5
2,
2.5
At the same time, I do not want to check the file periodically, but I want that as soon as a new line appears, the value will immediately appear. Those. as tail -fsoon as with additional logic.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1nd1go, 2012-02-23
@1nd1go

Our problem. I cheated a little, the line I called was:
tail -f file.txt | awk '{..morestuff..; printf result;}' | awk '{total+=$0;count+=1;print total/count}'
It does not work because the first awk buffers the output and does not give it to the second. Therefore, it is right to do:
tail -f file.txt | awk '{..morestuff..; printf result; fflush()}' | awk '{total+=$0;count+=1;print total/count}'

Z
Zlobober, 2012-02-22
@Zlobober

Something I don’t stick in, what kind of average do you think if your third number is 3, and the fourth is 3.5.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question