BASH SHELL SCRIPT to split a big xml file into multiple small files

12,296

Solution 1

Not pure answer but you can tune this yourself:

csplit -ksf part. src.xml /\<child\>/ "{100}" 2>/dev/null

This command will split src.xml using regexp /\<child\>/ as a delimiter and produce 1..100 part.* files. You need to play with regexp though...

Solution 2

One solution is to write a XSL file and use xsltproc with the stylesheet and the xml file to generate the single files.

See How to split XML file into many XML files using XSLT for an example.

Share:
12,296
Balaji
Author by

Balaji

Updated on June 09, 2022

Comments

  • Balaji
    Balaji almost 2 years

    I have a an XML file in below format

    <?xml version="1.0" encoding="utf-8" ?>
    <parent>
        <child>
            <code></code>
            <text></text>
        </child>
        <child>
            <code></code>
            <text></text>
        </child>
     </parent>
    

    I need a BASH SHELL script to split this main xml file into multiple small XML files which should have contents from the <child> to </child> tag. File names could be parent file name plus a running serial number such as _1 for ex:20110721_1.xml etc.. Please help me with the script.

  • NilColor
    NilColor over 12 years
    But it's good to use it in bash script to split a file. Ah, and thanks for -1 me...
  • Nels Beckman
    Nels Beckman almost 10 years
    Hey, this command is great if you happen to know your XML has regular structure. Is there any way to divide the original file into evenly sized (roughly) files?
  • Mr.TK
    Mr.TK over 9 years
    This method is awesome - thank You for that NilColor
  • Will
    Will over 7 years
    Most useful thanks. Using -b%02d.xml adds a nice .xml on the end.