I need to select certain lines from the log file and also save them to a text file.
I tried the following but none of them are working as expected. The file "todel.txt" shows 0 bytes.
tail -f general.log | grep Some_word > >(tee -a todel.txt)
tail -f general.log | grep Some_word ; tee todel.txt
tail -f general.log | grep Some_word | tee -a todel.txt
Best Answer
You need to add
stdbuf(1)
into your pipeline:This will set
grep
's stdout stream buffering mode to be line-buffered, otherwisegrep
waits to get at least 4096 bytes from the stream (this is the default on Linux for buffered i/o).Alternatively, you can also call
grep
with--line-buffered
:See Turn off buffering in pipe and http://www.pixelbeat.org/programming/stdio_buffering/ for in-detail explanations.