XML - Split And Extract String between Chars

Hi,

I am trying to read the records from file and split into multiple files.
SourceFile.txt

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>John</Recipient><BookID>20121212160208080</BookID><Created>2012-12-12T16:02:08.080-08:00</Created><VName>VN_010203.xml</VName></Info>........
...........................
............................</BOOK>
<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>zz</Recipient><BookID>20111212160208080</BookID><Created>2011-12-12T16:02:08.080-08:00</Created><VName>VN_010004.xml</VName></Info>........
...........................
............................</BOOK>
<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>bb</Recipient><BookID>20101212160208080</BookID><Created>2010-12-12T16:02:08.080-08:00</Created><VName>VN_000001.xml</VName></Info>........
...........................
............................</BOOK>
<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>J</Recipient><BookID>20091212160208080</BookID><Created>2009-12-12T16:02:08.080-08:00</Created><VName>VN_999999.xml</VName></Info>........
...........................
............................</BOOK>
<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>cc</Recipient><BookID>20071212160208080</BookID><Created>2007-12-12T16:02:08.080-08:00</Created><VName>VN_011111.xml</VName></Info>........
...........................
............................</BOOK>
<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>dd</Recipient><BookID>20081212160208080</BookID><Created>2008-12-12T16:02:08.080-08:00</Created><VName>VN_022222.xml</VName></Info>........
...........................
............................</BOOK>

My Output should be:
VN_010203.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>John</Recipient><BookID>20121212160208080</BookID><Created>2012-12-12T16:02:08.080-08:00</Created><VName>VN_010203.xml</VName></Info>........
...........................
............................</BOOK>

VN_010004.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>zz</Recipient><BookID>20111212160208080</BookID><Created>2011-12-12T16:02:08.080-08:00</Created><VName>VN_010004.xml</VName></Info>........
...........................
............................</BOOK>

VN_000001.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>bb</Recipient><BookID>20101212160208080</BookID><Created>2010-12-12T16:02:08.080-08:00</Created><VName>VN_000001.xml</VName></Info>........
...........................
............................</BOOK>

VN_999999.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>J</Recipient><BookID>20091212160208080</BookID><Created>2009-12-12T16:02:08.080-08:00</Created><VName>VN_999999.xml</VName></Info>........
...........................
............................</BOOK>

VN_011111.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>cc</Recipient><BookID>20071212160208080</BookID><Created>2007-12-12T16:02:08.080-08:00</Created><VName>VN_011111.xml</VName></Info>........
...........................
............................</BOOK>

VN_022222.xml

<?xml version="1.0" encoding="UTF-8"?>
<BOOK><Info><Sender>O'Relly</Sender><Recipient>dd</Recipient><BookID>20081212160208080</BookID><Created>2008-12-12T16:02:08.080-08:00</Created><VName>VN_022222.xml</VName></Info>........
...........................
............................</BOOK>

below is the code what I have tried so far but I am unable to store the Filename, is there any way to get the solution in a single script or command.

awk 'NR%2==1{x="VName_" ++i;}{print > x}' input
 
awk -F"[\>\<]" '/<VName>/ {print $5}' input
 

Thanks in advance
--Ulf

awk -F "<VName>|</VName>" '{s=s?s"\n"$0:$0}
/^<BOOK><Info>/{fn=$2}
/<\/BOOK>$/{print s > fn ;s=""}' file
1 Like

Thanks a lot its working fine