I'm trying to check new lines in a file using tail, then prepend each new line with something using sed, and print the output to the standard out.
The problem is, after adding a new line to the file, that line is not printed, but a previous one instead.
I am a Linux noob. So far I tried to pipe the output of sed to another program, like echo, printf, cat, more, less, but none of these worked.
I also tried adding "p" as a pattern flag at the end of the sed string, but that causes to print each string twice.
tail -F /usr/local/logs/myfile.log | sed -e "s/^/[hostname:$HOSTNAME]
[file:myfile.log] /"
I open another terminal and enter:
echo "test1" >> /usr/local/logs/myfile.log
(nothing happens, I expected message with "test1" to be printed)
echo "test2" >> /usr/local/logs/myfile.log
(prints [hostname:5b1dc0d27a45] [file:myfile.log] test1
, I expect "test2")
If I only use tail, without sed, each new line is displayed properly - but of course not prepended with the stuff I wanted.
You use tail with follow mode. So the input for sed is not closed. For optimization, output is done in blocks (buffered) or at end. sed -u ...
should help: It changes the output mode to unbuffered.