7a8408af6bd23e4ccfe1cca3bfc89921febc48df
[migration-tools.git] / oneliners / spit_tag_multiplication.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 my $filetype = $ARGV[0]; # XML or USMARC
8 my $filename = $ARGV[1];
9 my $tag1 = $ARGV[2]; # use NONE for no subfield, such as in 001
10 my $subfield1 = $ARGV[3];
11 my $tag2 = $ARGV[4];
12 my $subfield2 = $ARGV[5];
13
14 die "required arguments: filename tag1 subfield1 tag2 subfield2\n" if (! ($filename && $tag1 && $subfield1 && $tag2 && $subfield2) );
15
16 my $count = 0;
17
18 binmode(STDOUT, ':utf8');
19 binmode(STDIN, ':utf8');
20
21 print STDERR "Processing $filename\n";
22
23 my $batch = MARC::Batch->new($filetype,$filename); $batch->strict_off(); $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 @tags1 = (); if ($record->field($tag1)) { @tags1 = $record->field($tag1); } else { next; }
32
33     foreach my $f1 ( @tags1 ) {
34         if ($subfield1 eq 'NONE' ) {
35
36                 #***********************************************************************************************************************
37
38                     my @tags2 = (); if ($record->field($tag2)) { @tags2 = $record->field($tag2); } else { next; }
39
40                     foreach my $f2 ( @tags2 ) {
41                         if ($f2->subfield($subfield2)) {
42                             my @subfields2 = $f2->subfield($subfield2);
43                             foreach my $s2 ( @subfields2 ) {
44                                 print $f1->as_string() . "\t$s2\n";
45                             }
46                         }
47                     }
48
49                 #***********************************************************************************************************************
50
51         } else {
52             if ($f1->subfield($subfield1)) {
53                 my @subfields1 = $f1->subfield($subfield1);
54                 foreach my $s1 ( @subfields1 ) {
55                 #***********************************************************************************************************************
56
57                     my @tags2 = (); if ($record->field($tag2)) { @tags2 = $record->field($tag2); } else { next; }
58
59                     foreach my $f2 ( @tags2 ) {
60                         if ($f2->subfield($subfield2)) {
61                             my @subfields2 = $f2->subfield($subfield2);
62                             foreach my $s2 ( @subfields2 ) {
63                                 print "$s1\t$s2\n";
64                             }
65                         }
66                     }
67
68                 #***********************************************************************************************************************
69                 }
70             }
71         }
72     }
73 }
74 print STDERR "Processed $count records\n";