Skip to content

Instantly share code, notes, and snippets.

@marekkalnik
Created March 9, 2012 11:08
Show Gist options
  • Save marekkalnik/2006101 to your computer and use it in GitHub Desktop.
Save marekkalnik/2006101 to your computer and use it in GitHub Desktop.
AWK - split XML subnodes into separate files
# This awk script splits an XML file and exports each node into separate file
# It doesn't work if the main node has an attribute
# It doesn't work if the main node contains a subnode with the same tagname, etc.
# Use at your own risk, think before using.
# Usage - replace "node" by your tag and run: $ awk -f split-xml.awk my.xml
/<node>/ {
rfile="node" count ".xml"
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > rfile
print $0 > rfile
getline
while ($0 !~ "<\/node>" ) {
print > rfile
getline
}
print $0 > rfile
close(rfile)
count++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment