Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'grep' a continuous stream?

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.

like image 223
Matthieu Napoli Avatar asked Aug 23 '11 13:08

Matthieu Napoli


People also ask

Does grep buffer?

1 Answer. When using non-interactively, most standard commands, include grep , buffer the output, meaning it does not write data immediately to stdout . It collects large amount of data (depend on OS, in Linux, often 4096 bytes) before writing.

Can you pipe to grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".


2 Answers

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

It will wait till grep flushes, not till it finishes (I'm using Ubuntu).

like image 38
Irit Katriel Avatar answered Sep 27 '22 00:09

Irit Katriel


Turn on grep's line buffering mode when using BSD grep (FreeBSD, Mac OS X etc.)

tail -f file | grep --line-buffered my_pattern 

It looks like a while ago --line-buffered didn't matter for GNU grep (used on pretty much any Linux) as it flushed by default (YMMV for other Unix-likes such as SmartOS, AIX or QNX). However, as of November 2020, --line-buffered is needed (at least with GNU grep 3.5 in openSUSE, but it seems generally needed based on comments below).

like image 76
tad Avatar answered Sep 26 '22 00:09

tad