Here's my command that I'm using in a script to grep
real-time data. It doesn't seem to pull real-time data correctly as it just misses some lines.
tail -f <file> | fgrep "string" | sed 's/stuff//g' >> output.txt
What would the following command do? What is "line buffering"?
tail -f <file> | fgrep --line-buffered "string" | sed 's/stuff//g' >> output.txt
When using non-interactively, most standard commands, include
grep
, buffer the output, meaning it does not write data immediately tostdout
. It collects large amount of data (depend on OS, in Linux, often 4096 bytes) before writing.In your command,
grep
's output is piped tostdin
ofsed
command, sogrep
buffer its output.So,
--line-buffered
option causinggrep
using line buffer, meaning writing output each time it saw a newline, instead of waiting to reach 4096 bytes by default. But in this case, you don't needgrep
at all, just usetail
+sed
:With command that does not have option to modify buffer, you can use GNU coreutils stdbuf
to turn on line buffering or using
-o0
to disable buffer.Note