Reputation: 15482
I have this xml file:
<xml>
<segmentrecorddb>
...
</segmentrecorddb>
</xml>
I want to write a sh file that will add an inner xml node
meaning the file will be:
<xml>
<segmentrecorddb>
<myNoed>
</myNoed>
...
</segmentrecorddb>
</xml>
how can I delete the first two lines
add the 2 lines and add
Edit1:
I have tried:
➜ Downloads sed "s/<xml><segmentrecorddb>/<xml><segmentrecorddb><myNode></myNode>/" menyHtml.html > menyHtml.html
and got:
sed: 1: "s/<xml><segmentrecorddb ...": bad flag in substitute command: 'm'
Edit2:
<xml>
<segmentrecorddb>
...
</segmentrecorddb>
</xml>
I ran
sed 's/<segmentrecorddb>/<segmentrecorddb>\n<myNode><\/myNode>/' < menyHtml.html > menyHtml1.html
for the input
and got output:
<xml>
<segmentrecorddb>n<myNode></myNode>
...
</segmentrecorddb>
</xml>
how can I fix this?
Upvotes: 0
Views: 120
Reputation: 6576
Shell scripts are a poor choice for manipulating XML robustly, as the string processing utilities available for UNIX tend to be line oriented, and XML is not line oriented.
If you produce a solution that implicitly relies on where line breaks exist, for example, you'll find that your solution could break when someone changes how the input XML gets normalised.
You may also need to worry about whether you want to alter every instance of <segmentrecorddb>
or just one.
There are some tools that can help e.g. xml2 and pyxie. However, if you need to install something else, why not install something that can handle XML properly (e.g. Python)?
That said, the following sed command will do the fix in the most simplistic way:
sed 's/<segmentrecorddb>/<segmentrecorddb>\n<myNode><\/myNode>/' < input.xml > output.xml
Upvotes: 1