--- /dev/null
+#!/usr/bin/perl
+
+=head1 NAME
+
+cgrep - grep the output of a command, only printing stdout/stderr if match
+
+=head1 SYNOPSIS
+
+cgrep [-v] PATTERN COMMAND...
+
+=head1 DESCRIPTION
+
+cgrep runs a command, buffering the output from both standard out and
+standard error, looking for a matching pattern. If a matching pattern
+is found, stdout and stderr are printed and cgrep exits with the exit
+code of COMMAND.
+
+If the -v switch is passed, cgrep switches into reverse mode where it
+will print if there are any lines not matching PATTERN.
+
+=head1 AUTHOR
+
+Copyright 2010 by Collabora Limited, written by Tollef Fog Heen
+<tollef.fog.heen@collabora.co.uk>
+
+Licensed under the GNU GPL.
+
+=cut
+
+use warnings;
+use strict;
+use IPC::Run qw( start pump finish timeout );
+
+$|=1;
+
+my $reverse = 0;
+use Getopt::Long;
+GetOptions("v" => \$reverse) || die "usage: cgrep [-v] PATTERN COMMAND...\n";
+
+my $pattern = shift @ARGV;
+my @command = @ARGV;
+
+my ($in, $out, $err);
+my $h = IPC::Run::start \@command, \*STDIN, \$out, \$err;
+
+$h->finish;
+
+if ($reverse) {
+ if ($out !~ /$pattern/m and $err !~ /$pattern/m) {
+ print STDOUT $out;
+ print STDERR $err;
+ }
+} else {
+ if ($out =~ /$pattern/m or $err =~ /$pattern/m) {
+ print STDOUT $out;
+ print STDERR $err;
+ }
+}
+exit $h->result or 0;