Reputation: 39
I want to escape double quotes inside a XML element. For instance
FROM
<person name="Tiberius Claudius "Maximus"" sex="M">
TO
<person name="Tiberius Claudius "Maximus"" sex="M">
I was able to isolate the attribute value using sed:
$ cat sample.xml | sed -r 's/(<person name=")(.*)(" sex.*)/\2/'
Tiberius Claudius "Maximus"
Is there a way to replace double quotes "
with "
within the second group?
Upvotes: 1
Views: 274
Reputation: 385754
perl -i~ -pe's{<person name="\K(.*?)(?=" sex)}{ $1 =~ s/"/"/gr }eg' sample.xml
Or if you don't have 5.14,
perl -i~ -pe's{<person name="\K(.*?)(?=" sex)}{ ( my $s = $1 ) =~ s/"/"/g; $s }eg' sample.xml
Upvotes: 1