#!/usr/bin/perl use strict; my $Usage = "Usage: $0 value file1 file2\n"; die $Usage unless ( @ARGV == 3 and -f $ARGV[1] and -f $ARGV[2] ); my $value = shift; # removes first element from @ARGV my @match; # will hold matching line from each file for my $file ( @ARGV ) { # loop over remaining two ARG's open( IN, $file ) or die "$file: $!"; while () { if ( /$value/ ) { chomp; push @match, $_; last; } } } print join( " ", @match ), "\n"; #### grep value file1 file2 #### perlscript value file1 file2 > matched.lines # or grep value file1 file2 > matched.lines