#!/usr/bin/env perl
#
# grepc - grep for pattern and cut it
#
# Examples:
#
# get ipaddress of interface ppp0:
#   $ ifconfig ppp0 | grepc 'inet addr:(.*?)\s'
#
# list all debian packages which have dependency on a given package
#   $ apt-cache showpkg perl-tk | grepc '(.*?),perl-tk'
#
# list all files which have been changed in comparison to the
# cvs repository:
#   $ cvs diff 2>/dev/null | grepc '^RCS file: (.*?),v$'
#
# get environment variable of a specific process:
#   $ grepc 'PATH=(.*?)\0' </proc/<pid>/environ
#
# list destination ports of iptables lol:
#   $ </var/log/messages grepc 'DPT=(.*?) '
#
# get debian packages on host1, download them on host2:
#   host1:$ apt-get install --yes --force-yes --print-uris gnome >uris.txt
#   host2:$ <uris.txt grepc "^'(.*?)'" | xargs -l ncftpget
#
# list all directories containing apache-java source:
#   $ find . -path '*org/apache/*.java' | grepc '^(.*?)/org/apache' | uniq
#
# ---
# source: https://github.com/jkirk/grml-scripts

use strict;
use warnings;
use utf8;
use Encode         qw(decode_utf8);
use File::Basename qw(basename);

my $cmd_name = basename($0);
my $re       = shift
  or die "usage: $cmd_name <perl regexp containing a capture buffer>\n";

$re = eval { qr/$re/ } or die "invalid regexp: $@";

while (<>) {
    if (/$re/) {
        print "$1\n";
    }
}
