spin on argument
[migration-tools.git] / yaz-cleanup
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $skip = shift || 0;
7 my $count = 0;
8 $| = 1;
9
10 open MARC, '<', 'incoming.marc.xml';
11 open NUMARC, '>', 'incoming.clean.marc.xml';
12
13 until ($count == ($skip - 1)) {
14     my $t = <MARC>;
15     print NUMARC $t;
16     $count++;
17     printf("\rSpinning on to record %s (%2.2f%%)", $skip, ($count / $skip *100))
18       unless ($count % 1000);
19 }
20 print "\nScrubbing resumes...\n" if $skip;
21
22 my $line1 = <MARC>;
23
24 while (my $line2 = <MARC>) {
25     $count++;
26     # catch empty datafield elements
27     if ($line1 =~ m/<datafield tag="..." ind1="." ind2=".">/) {
28         if ($line2 =~ m|</datafield>|) {
29             $line1 = <MARC>;
30             $count++;
31             next;
32         }
33     }
34
35     # clean up tags with spaces in them
36     $line1 =~ s/tag="  /tag="00/g;
37     $line1 =~ s/tag=" /tag="0/g;
38     $line1 =~ s/tag="-/tag="0/g;
39     $line1 =~ s/tag="(\d\d) /tag="0$1/g;
40
41     # subfields can't be non-alphanumeric
42     die "Junk in subfield at line $count: $line1"
43       if $line1 =~ /<subfield code="[^[:alnum:]]"/;
44
45     # everything looks ok
46     print NUMARC $line1;
47     $line1 = $line2;
48 }
49 print NUMARC $line1;