fingerprinter tweak
[migration-tools.git] / split_marc.pl
1 #!/usr/bin/perl
2 use open ':utf8';
3 use MARC::Batch;
4 use MARC::Record;
5 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
6 use MARC::Field;
7 use POSIX;
8 use Error qw/:try/;
9
10 my $split_every = $ARGV[0];
11 my $count = 0;
12
13 binmode(STDOUT, ':utf8');
14 binmode(STDIN, ':utf8');
15 my $M;
16
17 foreach $argnum ( 1 .. $#ARGV ) {
18         
19         open $M, '<:utf8', $ARGV[$argnum];
20
21         print STDERR "Processing " . $ARGV[$argnum] . "\n";
22
23         my $batch = MARC::Batch->new('XML', $M);
24         $batch->strict_off();
25         $batch->warnings_off();
26
27         my $record;
28         while ( try { $record = $batch->next() } otherwise { $record = -1 } ) {
29                 next if ($record == -1);
30                 $count++;
31
32                 my $filename = $ARGV[$argnum] . ".split." .  floor( $count / $split_every ) . ".xml";
33
34                 open FILE, ">>$filename";
35                 binmode(FILE, ':utf8');
36                 print FILE $record->as_xml();
37                 close FILE;
38
39                 $record = undef;
40
41                     unless ($count % 1000) {
42                         print STDERR "$count\r"
43                 }
44
45         }
46     print STDERR "Processed $count records.\n";
47 }
48