<div>
...
</div>
<div > _
<div https://metacpan.org/pod/XML::LibXML" rel="nofollow noreferrer">XML::LibXML
in Perl to extract the innermost div
tags which contains text with "Text1":
use feature qw(say);
use strict;
use warnings;
use XML::LibXML;
my $xml = '
<sample>
<div>
some text
</div>
<div >
<div >
Text1
</div>
</div>
</sample>';
my $doc = XML::LibXML->load_xml(string => $xml);
for my $node ($doc->findnodes('//div[not(descendant::div)]')) {
#my $txt = $node->toString();
my $txt = $node->textContent();
say $txt if $txt =~ /\QText1\E/;
}
Output:
Text1
CodePudding user response:
Yes Perl can do this, for quick command-line hacks. For serious use (programming) see the answer by Håkon Hægland.
perl -e 'undef $/; $_=<>; /.*(<.*?Text1.*?>)/s; print "$1\n"' < infile
With your example as infile
, the output is:
<div row">
</div>