#!/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", $&; } } }