linux - [Solved-5 Solutions] How to grep a continuous stream - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

Is that possible to use grep on a continuous stream ?

Linux - Solution 1:

Turn on grep's line buffering mode.

tail -f file | grep --line-buffered my_pattern
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

You can use the tail -f <file> | grep <pattern> all the time.

It will wait till grep flushes, not till it finishes.

Linux - Solution 3:

grep uses some output buffering. You can try

tail -f file | stdbuf -o0 grep my_pattern
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

You can tail -f /var/log/some.log |grep foo and it will work just fine.

If you need to use multiple greps on a running log file and you find that you get no output, you may need to stick the --line-buffered switch into your middle grep(s), like so:

tail -f /var/log/some.log | grep --line-buffered foo | grep bar
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

tail -F <fileName> | grep --line-buffered  <pattern> -A 3 -B 5
click below button to copy the code. By - Linux tutorial - team

-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


Related Searches to - linux - linux tutorial - How to grep a continuous stream