Subject: Re: multiple greps, buffering, regular expressions
To: Jeremy C. Reed <reed@reedmedia.net>
From: Giles Lean <giles@nemeton.com.au>
List: netbsd-users
Date: 05/03/2001 08:42:00
> I want to be able to easily use multiple greps, like:
> 
> tail -f logfile | grep keyword | grep -v exclude-keyword
> 
> But due to the stdio buffering I may not (quickly) receive a result.

tail -f logfile | awk '/keyword/ && !/exclude-keyword/ { print; fflush() }'

fflush() is a non-standard GNU awk function.  Less flexible but more
portable (I think :-) is this, but you can't pipe the awk output:

tail -f logfile | awk '/keyword/ && !/exclude-keyword/ { print > "/dev/tty" }'

Personally I'd use perl:

tail -f logfile |
    perl -ne '$| = 1; print if /keyword/ and not /exclude-keyword/'

Regards,

Giles