Skip to content

Instantly share code, notes, and snippets.

@pkdavies
Created March 13, 2013 10:30
Show Gist options
  • Save pkdavies/5150902 to your computer and use it in GitHub Desktop.
Save pkdavies/5150902 to your computer and use it in GitHub Desktop.
Perl script for XML to TSV (tab separated) allowing fast MySQL import
#!/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