This is more a conceptual thing than a current problem.
Would it be possible to do this:
tail -f <file> | grep "string" | sed 's/stuff//g' >> output.txt
The problem I'm thinking is that tail -f
never terminates so it won't progress in the command. I'm trying to read and output a file in real-time. If this doesn't work how would I do it?
Most commands that output data on stdout, (which includes
grep
andsed
,) buffer their output when it's not going to a terminal. That is, they wait for a large chunk of output to gather (like 4KiB) before flushing the chunk to the file or pipe. This is generally more efficient. Some commands allow you to override this, like GNUgrep
which has a--line-buffered
option, and GNUsed
has an--unbuffered
option. For commands that don't have a way to override the buffering, you can use tools likeunbuffer
orstdbuf
.In this case though, I'd consider using GNU
awk
instead, where you can control the flushing of stdout yourself (with the fflush function).See BashFAQ #9 for more on buffering.