use HTML::TokeParser; open($a, "samplehtml.txt") or die("cannot open infile: $!"); $p = HTML::TokeParser->new($a); while (my $token = $p->get_tag("div")) { $text = $p->get_text("/div"); foreach ($text){ @body = grep /FAIL/, $text; print @body, "\n";} }