Is that possible to use grep
on a continuous stream?
What I mean is sort of a tail -f <file>
command, but with grep
on the output in order to keep only the lines that interest me.
I've tried tail -f <file> | grep pattern
but it seems that grep
can only be executed once tail
finishes, that is to say never.
you may consider this answer as enhancement .. usually I am using
-F is better in case of file rotate (-f will not work properly if file rotated)
-A and -B is useful to get lines just before and after the pattern occurrence .. these blocks will appeared between dashed line separators
Yes, this will actually work just fine.
Grep
and most Unix commands operate on streams one line at a time. Each line that comes out of tail will be analyzed and passed on if it matches.If you want to find matches in the entire file (not just the tail), and you want it to sit and wait for any new matches, this works nicely:
The
-c +0
flag says that the output should start0
bytes (-c
) from the beginning (+
) of the file.I think that your problem is that grep uses some output buffering. Try
it will set output buffering mode of grep to unbuffered.
This one command workes for me (Suse):
collecting logins to mail service
Didn't see anyone offer my usual go-to for this:
I prefer this, because you can use
ctrl + c
to stop and navigate through the file whenever, and then just hitshift + f
to return to the live, streaming search.