summaryrefslogtreecommitdiffstats
path: root/grep-group
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2016-12-26 11:25:56 +0100
committerFlorian Pritz <bluewind@xinu.at>2016-12-26 11:25:56 +0100
commit6c4b01440c0d4109b063677712d21740133eed4f (patch)
tree743ceec5ca371233d401d05e830445906c307192 /grep-group
parent9f902db6b0443e26b2886d81bae5e87a10531a6c (diff)
downloadbin-6c4b01440c0d4109b063677712d21740133eed4f.tar.gz
bin-6c4b01440c0d4109b063677712d21740133eed4f.tar.xz
Add grep-group
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'grep-group')
-rwxr-xr-xgrep-group31
1 files changed, 31 insertions, 0 deletions
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), " <regexp>\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", $&;
+ }
+ }
+}