use strict; open (INP,"data.txt") || die "Cannot open data.txt\n"; while (my $line = ) { if ($line=~/^This\b/i) { # first word This if ($line !~ /\bfirst\b/i) { # without word first if ($line =~ /\.$/) { # . at the end print $line; } } } }