I am trying to split a large xml file into several smaller files. I found a solution to split each node into it’s own file:
awk '/<mono/{close("row"count".xml");count++}count{f="row"count".xml";print $0 > f}' file.xml
The above code matches every “mono” node and outputs it to a file names row{rownumber}.xml. How can I print every 20 matches to a file?
I would say keep your “count” variable, and you just need to change the way you build your filename:
f="row" int(count/20) ".xml"You don’t have to explicitly close the file. All open files will be closed when awk exits.Given the comments, I’ll strike that remark. Note in the code below, a file will be closed up to 20 times, but reopened as required.