#!/bin/sh

# Run a command and send all its output (stdout and stderr) through a list
# of grep-compatible extended regular expressions. Any lines of the
# command's output that match one of the regular expressions in that list
# will be removed while all the other lines will be sent to stdout.

# Print usage and quit
usage(){
    echo "$error"
    echo "Usage:   $0 <file_of_grep_eregexes> <command>"
    echo "Example: $0 /tmp/filter.eregex 'cat /etc/services'"
    exit 1
}

if [ $# -lt 2 ]; then
  error='Wrong number of arguments.'
  usage
fi

exec $2 2>&1 | grep -E -v -f $1

exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Adam Lewenberg

=head1 NAME

run-and-filter - Send a command's output through filter-syslog

=head1 SYNOPSIS

run-and-filter <file_of_grep_eregexes> <command>

=head1 DESCRIPTION

Run a command and send all its output (stdout and stderr) through a list
of grep-compatible extended regular expressions. Any lines of the
command's output that match one of the regular expressions in that list
will be removed while all the other lines will be sent to stdout.

Here is an example:

If C</tmp/filter.eregex> contains

    line [[:digit:]]\.$
    out 3

and you have a script C<myscript -a -g> whose output of your script is normally

    warning 1 at /tmp/test.pl line 4.
    stdout 1
    warning 2 at /tmp/test.pl line 7.
    stdout 2
    stdout 3
    warning 3 at /tmp/test.pl line 11.

then C<run-and-filter /tmp/filter.eregex 'myscript -a -g'> will output

    warning 3 at /tmp/test.pl line 11.
    stdout 1
    stdout 2

Here is an example of you might use this in a cron file:

    30 * * * * root /usr/bin/run-and-filter /tmp/filter.eregexes 'perl /tmp/test.pl'


Note that there is no facility for comments. If you attempt to put a
comment in the filter file, then this line will be interpreted as a
grep regular expression to match against.

=head1 AUTHOR

Adam Lewenberg <adamhl@stanford.edu>

=cut

__END_OF_DOCS__
