Generates a TSV with egid and MARCXML snippets to preserve for post-deduping reinsert...
authorBen Ostrowsky <ben@esilibrary.com>
Fri, 10 Sep 2010 17:48:17 +0000 (17:48 +0000)
committerGalen Charlton <gmc@esilibrary.com>
Mon, 16 Jul 2012 15:29:29 +0000 (11:29 -0400)
extract_xml_tags.pl [new file with mode: 0755]

diff --git a/extract_xml_tags.pl b/extract_xml_tags.pl
new file mode 100755 (executable)
index 0000000..404e3af
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/perl -w
+use strict;
+
+use Getopt::Long;
+
+my (@tags, $infile);
+GetOptions ("tags=s" => \@tags,
+            "infile=s" => \$infile);
+@tags = split(/,/, join(',', @tags));
+
+open(FH, $infile) or die "Can't open $infile for reading: $!";
+
+while (<FH>) { 
+
+  my %tag;
+  my $xml = $_;
+
+  # Find the Evergreen bib ID
+  $xml =~ m/<datafield tag="903".+?<subfield code="a">(.+?)<\/subfield>/; 
+  my $egid = $1; 
+
+  # Find each occurrence of each tag specified
+  foreach (@tags) {
+    $tag{$_} = [ $xml =~ m/(<datafield tag="$_".+?<\/datafield>)/g ];
+  }
+
+  # Clean up the results before printing
+  my $output = '';
+  foreach my $key (sort keys %tag) {
+    my $text = join("", @{$tag{$key}});
+    $text =~ s/>\s+</></g;
+    $output .= $text;
+  }
+
+  # If we found any specified tags, print what we found.
+  if ($output ne '') {
+    print "$egid\t$output\n";
+  }
+
+}
+
+close(FH);