#!/usr/bin/perl # file grep.pl my $sought = shift; # from @ARGV my $reference = shift; $sought && $reference or die "usage: $0 soughtstring refstring files\n"; my $found; while(<>) { chop; # strip newline /$sought/ and $found = $_; # or, if you want the very first occurence, don't # overwrite the variable (see perlop for '||='): # /$sought/ and $found ||= $_; if (/$reference/) { print "$ARGV: '$found'\n" if $found; $found = ''; } }