with Koha, you don't want to batch import authorities that have their own 001
[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 = $marc->field('001')->data(); # grabs the first
10     $marc->delete_fields(@cns); # deletes all of them
11     my @sources = $marc->field('003'); # etc
12     my $source = $marc->field('003')->data();
13     $marc->delete_fields(@sources);
14     my @tags035 = $marc->field('035');
15     my $tag035 = $marc->field('035');
16     my $tag035a = defined $tag035 ? $tag035->subfield('a') : undef;
17     $marc->delete_fields(@tags035);
18     if (defined $cn) {
19         my @arr = (
20             '035','','','a'
21
22         );
23         if (defined $source) {
24            push @arr, "($source) $cn";
25         } else {
26            push @arr, "$cn";
27         }
28         if (defined $tag035a) {
29             push @arr, 'z';
30             push @arr, $tag035a;
31         }
32         my $new035 = MARC::Field->new(@arr);
33         $marc->insert_fields_ordered($new035);
34     }
35     print $marc->as_usmarc();
36 }
37 $file->close();
38 undef $file;
39