#! perl
use strict;
use warnings;
use Tie::File;
my $filename = 'data.txt';
tie my @lines, 'Tie::File', $filename or die "Cannot tie file '$filena
+me': $!";
my @search_words = ('jumped', 'over');
for (0 .. $#lines - 1)
{
if ($lines[$_ ] =~ /$search_words[0]/ &&
$lines[$_ + 1] =~ /$search_words[1]/)
{
print "\nFound '$search_words[0]' on line ", ($_ + 1),
", and '$search_words[1]' on line ", ($_ + 2), "\n";
last;
}
}
untie @lines;
Output:
16:02 >perl 605_SoPW.pl
Found 'jumped' on line 5, and 'over' on line 6
16:03 >perl 605_SoPW.pl
Your precise requirements are not clear (to me), but this technique can be easily adapted as needed.
Hope that helps,
|