404e3af2685593e374ea02ee7d8e71b914793d77
[migration-tools.git] / extract_xml_tags.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use Getopt::Long;
5
6 my (@tags, $infile);
7 GetOptions ("tags=s" => \@tags,
8             "infile=s" => \$infile);
9 @tags = split(/,/, join(',', @tags));
10
11 open(FH, $infile) or die "Can't open $infile for reading: $!";
12
13 while (<FH>) { 
14
15   my %tag;
16   my $xml = $_;
17
18   # Find the Evergreen bib ID
19   $xml =~ m/<datafield tag="903".+?<subfield code="a">(.+?)<\/subfield>/; 
20   my $egid = $1; 
21
22   # Find each occurrence of each tag specified
23   foreach (@tags) {
24     $tag{$_} = [ $xml =~ m/(<datafield tag="$_".+?<\/datafield>)/g ];
25   }
26
27   # Clean up the results before printing
28   my $output = '';
29   foreach my $key (sort keys %tag) {
30     my $text = join("", @{$tag{$key}});
31     $text =~ s/>\s+</></g;
32     $output .= $text;
33   }
34
35   # If we found any specified tags, print what we found.
36   if ($output ne '') {
37     print "$egid\t$output\n";
38   }
39
40 }
41
42 close(FH);