I would like to follow changes to a file via
tail -f and then print out the first line that matches a
grep command before terminating. What's the simplest way to do this? So far I've been experimenting with things like:
tail -f foo.log | grep 'bar' | head -1
However, the pipeline just hangs, presumably due to buffering. I also tried
tail -f foo.log | grep --line-buffered 'bar' | head -1
This prints out the line, but the process does not terminate unless I hit ^C, presumably because a second line of input is needed to terminate
head -1. What's the best way to solve this problem?