Answer the question
In order to leave comments, you need to log in
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 -f
soon as with additional logic.
Answer the question
In order to leave comments, you need to log in
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}'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question