Rellix
Rellix

Reputation: 33

Unix - Split a file containing XML files into single files

I've been working on a piece of code that splits a file containing multiple XML files into individual XML files. The line count of each XML file varies so I have been using the XML head tag to know where the next file starts.

grep -n $string $xmlfile | sed -n 's/^\([0-9]*\)[:].*/\1p'

Which gets me the line number of the start of each file. How can I use the head/tail command to make use of the line numbers to pull the files apart within a single automated script?

Upvotes: 3

Views: 1169

Answers (2)

aefxx
aefxx

Reputation: 25249

// x1, x2 being XML declaration line numbers
cat myfile | head -n x2 | tail -n x1

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185073

When parsing xml files in your prefered shell, your best bet will be to use xmllint command-line and Xpath expressions.


xmllint comes from libxml.

See http://www.xmlsoft.org/ & http://en.wikipedia.org/wiki/Xpath

Upvotes: 0

Related Questions