strict_add_sf9
[migration-tools.git] / bibs_items / filter_out_mfhd.pl
1 #!/usr/bin/perl -w
2 # ./filter_out_mfhd.pl marcfile > out 2> err
3 # Looks for tcn_id.map2 containg lines like:  001_or_035value|eg_bib_id
4 # Spits out mfhd.tsv (eg_bib_id<tab>marcxml<tab>eg_bib_id) and mfhd.bad.mrc
5 # For marcfile, it expects a "title record", followed by one or more MFHD records.  Rinse, repeat.
6
7 use strict;
8 use warnings;
9 use open ':utf8';
10
11 use MARC::Batch;
12 use Unicode::Normalize;
13 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
14 use MARC::Field;
15
16 my $batch = MARC::Batch->new( 'USMARC', @ARGV );
17 $batch->strict_off();
18 $batch->warnings_off();
19
20 my $current_title;
21 my $tag001;
22 my $tag035;
23 my $tag245;
24 my $tag852;
25 my $tag866;
26 my %tcn2bid;
27
28 open FILE, "tcn_id.map2";
29 while (my $line = <FILE>) {
30     if ($line =~ /^(.+)\|(.*)$/) {
31         $tcn2bid{$1} = $2;
32     }
33 }
34 close FILE;
35
36 open MFHD, ">mfhd.tsv";
37 open BADMFHD, ">mfhd.bad.mrc";
38 while ( my $marc = $batch->next ) {
39     $tag001 = $marc->field('001');
40     $tag035 = $marc->field('035');
41     $tag245 = $marc->field('245');
42     $tag852 = $marc->field('852');
43     $tag866 = $marc->field('866');
44     if ($tag852 || $tag866) {
45         print "\tMFHD\n";
46         my $field = MARC::Field->new(
47             '004',
48             $tcn2bid{$current_title}
49             ? $tcn2bid{$current_title}
50             : 'missing: ' . $current_title
51         );
52         $marc->insert_fields_ordered( $field );
53         if ($tcn2bid{$current_title}) {
54             my $string = $marc->as_xml_record();
55             $string =~ s/\n//g;
56             $string =~ s/<\?xml version="1\.0" encoding="UTF-8"\?>//;
57             print MFHD $tcn2bid{$current_title} . "\t$string\t" . $tcn2bid{$current_title} . "\n";
58         } else {
59             print BADMFHD $marc->as_usmarc();
60         }
61     } else {
62         if ($tag001) {
63             my $tcnv = $tag001->as_string();
64             if ($tcnv =~ /^\d*$/) {
65                 print "fishy MFHD? with 001 $tcnv\n";
66                 print STDERR "=== fishy MFHD? with 001 $tcnv\n";
67                 print STDERR $marc->as_formatted() . "\n";
68             } else {
69                 print "title with 001 $tcnv, eg bib id = $tcn2bid{$tcnv}\n";
70                 $current_title = $tcnv;
71             }
72         } else {
73             if ($tag035) {
74                 my $tcnv = $tag035->as_string();
75                 print "title with 035 $tcnv, eg bib id = $tcn2bid{$tcnv}\n";
76                 $current_title = $tcnv;
77             } else {
78                 my $tcnv;
79                 if ($tag245) {
80                     $tcnv = $tag245->as_string();
81                 }
82                 print "fishy title? missing 001 and 035: $tcnv\n";
83                 print STDERR "=== fishy title? missing 001 and 035: $tcnv\n";
84                 print STDERR $marc->as_formatted() . "\n";
85                 $current_title = "fishy: $tcnv";
86             }
87         }
88     }
89 }
90 close BADMFHD;
91 close MFHD;