adding extract_holdings script
[migration-tools.git] / marc_cleanup
index f40babb..853de84 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/perl
+require 5.10.0;
 
 use strict;
 use warnings;
@@ -24,7 +25,7 @@ open my $NUMARC, '>:utf8', $conf->{output}
 open my $OLD2NEW, '>', 'old2new.map'
   if ($conf->{'renumber-from'} and $conf->{'original-subfield'});
 my $EXMARC = 'EX';
-
+print $NUMARC "<collection>\n";
 
 my @record  = (); # current record storage
 my %recmeta = (); # metadata about current record
@@ -87,18 +88,22 @@ while ( buildrecord() ) {
 
         # subfields can't be non-alphanumeric
         if ($record[$ptr] =~ /<subfield code="(.*?)"/) {
-            my $match = $1;
-            if ($match =~ /\P{IsAlnum}/ or $match eq '') {
+            if ($1 =~ /\P{IsAlnum}/ or $1 eq '') {
                 edit("Junk in subfield code/Null subfield code");
                 next;
             }
         }
+        # subfields can't be non-alphanumeric
+        if ($record[$ptr] =~ /<subfield code="(\w{2,})"/) {
+            edit("Subfield code larger than 1 char");
+            next;
+        }
 
         $ptr++;
     }
     write_record($NUMARC);
 }
-#print $NUMARC "</collection>\n";
+print $NUMARC "</collection>\n";
 print $OUT "\nDone.               \n";
 
 
@@ -174,7 +179,7 @@ sub do_automated_cleanups {
 
         # automatable subfield maladies
         $record[$ptr] =~ s/code=" ">c/code="c">/;
-        $record[$ptr] =~ s/code=" ">\$/code="c"$>/;
+        $record[$ptr] =~ s/code=" ">\$/code="c">\$/;
     }
 }
 
@@ -669,10 +674,9 @@ sub initialize {
     show_trashhelp() if ($c->{trashhelp});
 
     # defaults
-    if ($c->{prefix}) {
-        $c->{output} = join('.',$c->{prefix},'marc','xml');
-        $c->{exception} = join('.',$c->{prefix},'marc','ex');
-    }
+    my $pfx = $c->{prefix} // "bibs";
+    $c->{output} = join('.',$c->{prefix},'clean','marc','xml');
+    $c->{exception} = join('.',$c->{prefix},'exception','marc','xml');
     $c->{'renumber-tag'} = 903 unless defined $c->{'renumber-tag'};
     $c->{'renumber-subfield'} = 'a' unless defined $c->{'renumber-subfield'};
     $c->{window} = 5;
@@ -694,10 +698,8 @@ Options
   --output     -o  Cleaned MARCXML output filename
   --exception  -x  Exception (dumped records) MARCXML filename
        or
-  --prefix=<PREFIX>>   -p  Shared prefix for output/exception files. Will
-                           produce PREFIX.marc.xml and PREFIX.ex.xml
-
-  --trashfile  -t  File containing trash tag data (see --trashhelp)
+  --prefix=<PREFIX>>   -p  Shared prefix for output/exception files. Will produce
+                           PREFIX.clean.marc.xml and PREFIX.exception.marc.xml
 
   --renumber-from     -rf  Begin renumbering id sequence with this number
   --renumber-tag      -rt  Tag to use in renumbering (default: 903)
@@ -708,9 +710,11 @@ Options
                            and renumbering is in effect, an old-to-new mapping
                            file (old2new.map) will be generated.
 
-  --nocollapse -n  Don't compress records to one line on output
   --autoscrub  -a  Automatically remove non-numeric tags in data
+  --nocollapse -n  Don't compress records to one line on output
   --strip-nines    Automatically remove any existing 901/903 tags in data
+  --trashfile  -t  File containing trash tag data (see --trashhelp)
+
 
   --script         Store human-initiated ops in scriptfile (.mcscript)
                    Not yet implemented