I have a xml file with this structure from xmllint:
<Entities>
<Name>John</Name>
</Entities>
<Entities>
<Name>David</Name>
</Entities>
Now i would like to transform that file in something like:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<Entities>
<Name>John</Name>
</Entities>
<Entities>
<Name>David</Name>
</Entities>
</data>
How can I do that with sed? I tried this
sed -e '1i\<?xml version="1.0" encoding="UTF-8"?><data>' test.xml > test1.xml
But still lack the </data>
. Basically my idea is enclose the text from the test.xml with the header and a main tag called <data></data>
sed -e '1i\<?xml version="1.0" encoding="UTF-8"?><data>' -e '$a\</data>' test.xml > test1.xml
The main trick here is to put the $a\</data>
behind a -e
flag of its own so that it isn't interpreted as more raw text to insert before the first line.
That will give you valid XML. For the precise output in the question, use
sed -e '1i\<?xml version="1.0" encoding="UTF-8"?>' -e '1i\<data>' -e '$a\</data>' -e 's/^/ /'
or so. If it were me, though, I'd only use sed to make the data valid XML and work with XML tools from there (i.e., use the first snippet and pipe through xmllint for indentation). Handling structured data with sed is generally brittle, so you don't want to do it any longer than you have to.