bash - Adding XML element in XML file using sed command in shell script -
i using sed command insert xml element existing xml file.
i have xml file
<students> <student> <name>john</> <id>123</id> </student> <student> <name>mike</name> <id>234</id> </student> </students>
i want add new elememt
<student> <name>newname</name> <id>newid</id> </student>
so new xml file 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 have written shell script
#! /bin/bash content="<student> <name>newname</name> <id>newid</id> </student>" #sed -i.bak '/<\/students>/ \ "$content" /root/1.xml sed -i.bak '/<\/students>/ \'$content'/' /root/1.xml
i getting error as
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 xml file, <student>
added. remaining elements not added. know why error?
change :
content="<student> <name>newname</name> <id>newid</id> </student>"
to :
content="<student>\n<name>newname</name>\n<id>newid</id>\n</student>"
and :
c=$(echo $content | sed 's/\//\\\//g') sed "/<\/students>/ s/.*/${c}\n&/" file
Comments
Post a Comment