silence un-init string warning
[migration-tools.git] / filter1per.pl
1 #!/usr/bin/perl
2
3 use Time::HiRes qw/time/;
4 use MARC::Record;
5 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
6
7 my $tag = shift;
8 my $subfield = shift;
9 my $idfile = shift;
10 my $marcfile = shift;
11 my $found = shift;
12 my $notfound = shift;
13 if (! ($tag && $subfield && $idfile && $marcfile && $found ) ) {
14     print "filter1per.pl <tag> <subfield> <idfile> <marcfile> <output.found> [<output.notfound>]\n";
15     exit 0;
16 }
17
18 my %id;
19
20 open F, "<$idfile";
21 while (<F>) {
22         chomp;
23         $id{$_} = 1;
24 }
25
26 close F;
27
28 my $M;
29 open $M, '<:utf8', $marcfile;
30 open $I, '>:utf8', $found;
31 if ($notfound) { open $S, '>:utf8', $notfound; }
32
33 my $starttime = time;
34 my $count = 0;
35 my $icount = 0;
36 my $scount = 0;
37 while (<$M>) {
38
39         /tag="$tag" ind1=" " ind2=" ">.*?<subfield code="$subfield">(\d+)</;
40         if ( $id{$1} ) {
41                 print $I $_;
42                 $icount++;
43         } else {
44         if ($notfound) {
45                 print $S $_;
46         }
47                 $scount++;
48         }
49         $count++;
50
51         unless ($count && $count % 100) {
52                 print STDERR "\r$count\t(notfoundd: $scount, found: $icount)\t". $count / (time - $starttime);
53         }
54
55 }
56 print STDERR "\n";