preserve dates we can't parse; we'll punt to postgres
[migration-tools.git] / trim_marc_based_on_tag_subfield_value.pl
1 #!/usr/bin/perl
2 use MARC::Batch;
3 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
4 use MARC::Field;
5 use Unicode::Normalize;
6
7
8 my $tag_number = $ARGV[0];
9 my $tag_subfield = $ARGV[1];
10 my $tag_value = $ARGV[2];
11
12 my $count = 0;
13
14 binmode(STDOUT, ':utf8');
15 binmode(STDIN, ':utf8');
16
17 foreach $argnum ( 3 .. $#ARGV ) {
18
19         print STDERR "Processing " . $ARGV[$argnum] . "\n";
20
21         my $batch = MARC::Batch->new('XML',$ARGV[$argnum]);
22         $batch->strict_off();
23         $batch->warnings_off();
24
25         while ( my $record = $batch->next() ) {
26
27         $count++;
28
29                 print STDERR "WARNINGS: Record $count : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
30
31         my $keep_me = 0;
32
33         my @tags = ();
34                 my @tags; if ($record->field($tag_number)) { @tags = $record->field($tag_number); }
35                 foreach my $f ( @tags ) { 
36             if ($f->subfield($tag_subfield)) { 
37                 if ( $f->subfield($tag_subfield)=~ m/($tag_value)/i ) { $keep_me = 1; } 
38             } 
39         }
40
41         if ($keep_me) {
42             print STDOUT $record->as_xml();
43         }
44
45         }
46         print STDERR "Processed $count records\n";
47 }