Created
March 13, 2013 10:30
-
-
Save pkdavies/5150902 to your computer and use it in GitHub Desktop.
Perl script for XML to TSV (tab separated) allowing fast MySQL import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
### Begin | |
my $file = shift; | |
open (XML, "</home/scripts/cod/" . $file . ".xml") || die($!); | |
open (TSV, ">/home/scripts/cod/" . $file . ".tsv") || die($!); | |
my $header_end = 0; | |
while(my $line = <XML>) { | |
if(!$header_end) { | |
if($line =~ m/<root>/) { | |
$header_end = 1; | |
} | |
next; | |
} | |
if($line !~ m/<record>/ && $line =~ m/<(.*)>(.*)<\/\1>/) { | |
print TSV $2 . "\t"; | |
} | |
if($line =~ m/<\/record>/i) { | |
print TSV "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment