I use the sed command to insert an xml element into an existing xml file.
I have an xml file as
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
</Students>
I want to add a new item as
<student>
<name>NewName</name>
<id>NewID</id>
</student>
So my new xml file will be
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
<student>
<name>NewName</name>
<id>NewID</id>
</student>
</Students>
For this, I wrote a shell script as
#! /bin/bash
CONTENT="<student>
<name>NewName</name>
<id>NewID</id>
</student>"
sed -i.bak '/<\/Students>/ i \'$CONTENT'/' /root/1.xml
I get an error like
sed: can't read <name>NewName</name>: No such file or directory
sed: can't read <id>NewID</id>: No such file or directory
sed: can't read </student>: No such file or directory
And in the xml file is added only <student>. The remaining elements are not added. Does anyone know why this error?
source
share