I have two XML files and I want to
- find a specific XML node in File A
- copy it
- find a specific section in File B
- paste the copied node.
sed has been already used on my machine but I'm having troubles in finding the right regex configuration.
Example for File A:
<Containers>
<Container id="1"> <-- to be copied start
blubb
</Container> <-- to be copied end
<Container id="2">blobb</Container>
</Containers>
Example for File B:
<Containers>
<Container id="99">blibb</Container>
</Containers>
Example for needed output File B by cutting from <Container id="1"
to </Container>
:
<Containers>
<Container id="1"> <-- copied here start
blubb
</Container> <-- copied here end
<Container id="99">blibb</Container>
</Containers>
I do know that it would be much cleaner and maybe easier to use an XML parser and other tooling but I need to use sed and I'm not a very experienced sed/regex user. I just played around a little bit with "substitute" and "delete", but that's all...
May I clarify:
- I NEED to use sed since this is the only tool that is available on the machine.
- I do know and I'm able to do this in other programming languages and with other tools but this is not possible here. The machine where this shall run is not under my control!
I know I shouldn't be using regex for XML/XHTML - I do know but the earth is much more complicated.
I'm running this from cygwin.
Update 1:
Due to several responses it seems to be not possible to find a solution with sed. Thanks to all that understood the problem and tried to help!
If someone still sees a potential solution then please let me know. But the challenge is in using sed. I have used XML parsers with boost, QT, C#, Java, ... but that's simply not the problem here and if I could choose... I can't.
Update 2:
Thank you all and especially Benjamin W. It is definitely possible to use sed to solve this problem but as stated many times, if you have the possibility to use a xml parsing lib and an other technology then this should be the way to go.
For me, a non technical problem (pseudo security guideline) has been solved with the available technical solution.
This was my final solution:
sed "/<Container id=\"1\">/,/<\/Container>/!d" fileA.xml | ^
sed -i "/<Containers>/r /dev/stdin" fileB.xml
Thank you.