From 6c4b01440c0d4109b063677712d21740133eed4f Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 26 Dec 2016 11:25:56 +0100 Subject: Add grep-group Signed-off-by: Florian Pritz --- grep-group | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 grep-group (limited to 'grep-group') diff --git a/grep-group b/grep-group new file mode 100755 index 0000000..06bc923 --- /dev/null +++ b/grep-group @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use warnings; +use strict; +use File::Basename; + +if (@ARGV < 1) { + print "usage: cat | ", basename($0), " \n"; + exit 2; +} + +my $pattern = shift; + +while (<>) { + my $line = $_; + chomp($line); + + if (my @matches = $line =~ /$pattern/) { + if ($#- != 0) { + if (scalar(@matches) == 1) { + printf "%s\n", $matches[0]; + } else { + for my $match (@matches) { + print $match; + } + printf "\n"; + } + } else { + printf "%s\n", $&; + } + } +} -- cgit v1.2.3-24-g4f1b