let's not name these differently than the emig.d/ counterparts
[migration-tools.git] / munge_001_003_035.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use MARC::File::USMARC;
5
6 my $file = MARC::File::USMARC->in( $ARGV[0] );
7 while ( my $marc = $file->next() ) {
8     my @cns = $marc->field('001'); # grabs all of them
9     my $cn;
10     if (@cns) {
11         $cn = $marc->field('001')->data(); # grabs the first
12         $marc->delete_fields(@cns); # deletes all of them
13     }
14     my @sources = $marc->field('003'); # etc
15     my $source;
16     if (@sources) {
17         $source = $marc->field('003')->data();
18         $marc->delete_fields(@sources);
19     }
20     my @tags035 = $marc->field('035');
21     my $tag035 = $marc->field('035');
22     my $tag035a = defined $tag035 ? $tag035->subfield('a') : undef;
23     $marc->delete_fields(@tags035);
24     if (defined $cn) {
25         my @arr = (
26             '035','','','a'
27
28         );
29         if (defined $source) {
30            push @arr, "($source) $cn";
31         } else {
32            push @arr, "$cn";
33         }
34         if (defined $tag035a) {
35             push @arr, 'z';
36             push @arr, $tag035a;
37         }
38         my $new035 = MARC::Field->new(@arr);
39         $marc->insert_fields_ordered($new035);
40     }
41     print $marc->as_usmarc();
42 }
43 $file->close();
44 undef $file;