use strict; use warnings; open(IN, 'E:\test\infile.txt') || die $!; my @infile = ; close(IN); open(RE, 'E:\test\require.txt') || die $!; my @reqids = ; close(RE); chomp @infile; chomp @reqids; for my $index (0..$#infile){ for my $rindex (0..$#reqids){ if ($infile[$index] =~ m/$reqids[$rindex]/){ if ($infile[$index] =~ m/select/i){ my $count = $index; $count += 3; print "$infile[$_]\n" for ($index..$count); } } } } __END__ Output as: ---------- 15:53:43.908 Dbg 10739 Oracle: id='12.1' req='834531' SQL: SELECT crf_routing.CRF_RT_ACD1_QUEUE,crf_routing.CRF_RT_ACD1_SIZE,crf_ro' +0551 Executed SQL statement 'SELECT', start retrieve records... +0826 MSG_RETRIEVED2 status='DBM_SUCCESS' +0826 MSG_RETRIEVED status='DBM_NOMORE'