summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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", $&;
+ }
+ }
+}