1

I have many xml files which I want to merge into one file. I don't want to merge them from root but from one of the child. How to proceed with it using grep/sed/awk statements?

XML 1:

<root>
   <version>AB</version>
   <Data>
       <Title>MyTitle</Title>
       <SubTitle>Mysub</SubTitle>
   </Data>
   <file author="JXJX" name="MyFile1">
       <desc>File1</desc>
       <field>Random Field</field>
   </file>
<root>

XML 2:

<root>
   <version>AB</version>
   <Data>
       <Title>MyTitle 2</Title>
       <SubTitle>Mysub 2</SubTitle>
   </Data>
   <file author="HIGH" name="MyFile2">
       <desc>File2</desc>
       <field>Random Field</field>
   </file>
<root>

I want the following XML file:

<root>
    <file author="JXJX" name="MyFile1">
       <desc>File1</desc>
       <field>Random Field</field>
    </file>
    <file author="HIGH" name="MyFile2">
       <desc>File2</desc>
       <field>Random Field</field>
    </file>
</root>
Yankee
  • 17
  • 6

2 Answers2

1

Please don't parse XML with regex, but use a proper parser like instead:

$ xidel -se '
  element root {
    doc("1.xml")//file,
    doc("2.xml")//file
  }
' --output-node-format=xml --output-node-indent
<root>
  <file author="JXJX" name="MyFile1">
    <desc>File1</desc>
    <field>Random Field</field>
  </file>
  <file author="HIGH" name="MyFile2">
    <desc>File2</desc>
    <field>Random Field</field>
  </file>
</root>
Reino
  • 3,203
  • 1
  • 13
  • 21
0

For files as simple as your examples, you may get away with a short sed command which prints the first line, all lines from <file to </file>, and the last line:

sed -n '1p;/<file/,/<\/file>/p;$p' XML1 XML2
Armali
  • 18,255
  • 14
  • 57
  • 171
  • Hi @Armali, Thanks for the answer. In case I have an XML tag like .... . This will not work. Can you please tell how to modfiy the statement in such case. – Yankee Mar 17 '21 at 06:31
  • I just removed the space after ` – Armali Mar 17 '21 at 09:35