summaryrefslogtreecommitdiffstats
path: root/grep-group
blob: 06bc923883f956a5ec3f1ea508a3cf8eda8c1acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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", $&;
		}
	}
}