Hi Everyone,
I'm new here and I was checking this old post:
/shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html
(cannot paste link because of lack of points)
I need to do something like this but understand very little of perl.
I also check that maybe xml_split should do what I want but didn't really work, only split the first file and then crashed.
what I want is the following I have file X and need to split it into different files (file names are not relevant).
this would be my file content:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"><CstmrCdtTrfInitn><GrpHdr><MsgId>2504009000100320100060</MsgId></GrpHdr><PmtInf><PmtInfId>2014-10-06-09-39-23-305460</PmtInfId></PmtInf></CstmrCdtTrfInitn></Document><?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>WedDec0415:24:58CET2013</MsgId></GrpHdr></CstmrCdtTrfInitn></Document><?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.003.03 pain.001.003.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>MonOct0609:38:36CEST2014</MsgId></GrpHdr></CstmrCdtTrfInitn></Document>
what I expect at the end is to have something like this:
file1
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"><CstmrCdtTrfInitn><GrpHdr><MsgId>2504009000100320100060</MsgId></GrpHdr><PmtInf><PmtInfId>2014-10-06-09-39-23-305460</PmtInfId></PmtInf></CstmrCdtTrfInitn></Document>
file2
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>WedDec0415:24:58CET2013</MsgId></GrpHdr></CstmrCdtTrfInitn></Document>
file3
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.003.03 pain.001.003.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>MonOct0609:38:36CEST2014</MsgId></GrpHdr></CstmrCdtTrfInitn></Document>
at the moment I have only just started my code (very basic)
#!/usr/bin/perl -w
use Data::Dumper qw(Dumper);
# (1) quit unless we have the correct number of command-line args
$num_args = $#ARGV + 1;
if ($num_args < 1) {
print "\nUsage: split.pl filename\n";
exit;
}
$filename=$ARGV[0];
open INPUT, "<$filename";
@lines = <INPUT>;
close INPUT;
my @parts = split /<?xml version/, @lines;
print Dumper \@parts;
I was expecting to get maybe only the each line or something like that in my output but I'm getting the following:
[root@~]$ perl split.pl split.xml
$VAR1 = [
'16'
];
my split.xml file has 16 lines so I guess the split is giving me that it found 16 occurrences but I don't see any split being done. did I forget any parameter?