#!/usr/bin/perl use HTML::TreeBuilder::XPath; use strict; use warnings; my $root = HTML::TreeBuilder::XPath->new; $root->parse_file(\*DATA) or die("Could not parse the data: $!\n"); $root->eof(); my $xpath = '//div/p'; for my $d ($root->findnodes($xpath)) { my @line = $d->content_list; s/^\s+|\s+$//g for @line; $d->replace_with($line[0],qq(\n)); } print $root->as_trimmed_text,qq(\n); $root->delete; exit(0); __DATA__

foo00 bar00

foo01
bar01

foo02
bar02

foobar02 foo02a
bar02a

foo03
bar03
baz03

foo04
bar04
baz04

foo05

bar05
baz05