I'm using xmllint --shell
on a large XML file, and using the write
command to write out an XML snippet to be used in testing. The snippet that gets written out needs a couple of lines from the original XML file (the declaration, namespace, and the root node.) I want to be able to add these lines to the file, without having to manually copy the lines over. Instead, I'd like to use sed to append these lines for me so I can write a function to automate this very tedious task. To illustrate, this is a sample of what I'm trying to accomplish.
Source XML (source.xml):
<?xml version="1.0" encoding="UTF-8"?>
<foo:root xmlns:foo="TheFooNameSpaceIsImportant">
<foo:Entry>
<foo:SomeNode>Foo1</foo:SomeNode>
<foo:AnotherNode>Bar1</foo:AnotherNode>
</foo:Entry>
<foo:Entry>
<foo:SomeNode>Foo2</foo:SomeNode>
<foo:AnotherNode>Bar2</foo:AnotherNode>
</foo:Entry>
<foo:Entry>
<foo:SomeNode>Foo3</foo:SomeNode>
<foo:AnotherNode>Bar3</foo:AnotherNode>
</foo:Entry>
<!-- tens of thousands of others -->
<foo:Entry>
<foo:SomeNode>Foo20432</foo:SomeNode>
<foo:AnotherNode>Bar20432</foo:AnotherNode>
</foo:Entry>
</foo:root>
Saved XML snippet (sample.xml):
<foo:Entry>
<foo:SomeNode>Foo</foo:SomeNode>
<foo:AnotherNode>Bar</foo:AnotherNode>
</foo:Entry>
So I need to wrap this with the top two lines and the bottom line of the source.xml. But the following fails due to the <
character:
$ sed -i 1i"`head -n 2 source.xml`" sample.xml
sed: -e expression #1, char 43: unknown command: `<'
Is there a way to escape this character when it's being fed from a sub-command like this?
-i
? – 123 Mar 11 '16 at 15:36