$/="\n--\n"; my $value; while (<>) { my @arr = split "\n"; if (@arr >= 3 and $arr[-3] =~ /ABC SET/) { $value++; } }