removing miker-filter series; superceded by filter_record_ids, which has far less...
[migration-tools.git] / filter_record.ids
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Getopt::Long;
6 #use MARC::Record;
7 #use MARC::File::XML ( BinaryEncoding => 'utf-8' );
8
9 # configuration hashref
10 my $conf  = {};
11 initialize($conf);
12
13 my %id;
14
15 open F, "<", $conf->{idfile};
16 while (<F>) {
17         chomp;
18         $id{$_} = 1;
19 }
20 close F;
21
22 my $M; my $I; my $S;
23 open $M, '<:utf8', $conf->{marcfile}
24   or die "Can't open marcfile '",$conf->{marcfile},"'\n";
25 open $I, '>:utf8', $conf->{'output-import'}
26   or die "Can't open import file '",$conf->{'output-import'},"'\n";
27 open $S, '>:utf8', $conf->{'output-shelved'}
28   or die "Can't open shelf file '",$conf->{'output-shelve'},"'\n";
29
30 while (<$M>) {
31     my $tag = $conf->{tag};
32     my $sub = $conf->{subfield};
33
34     /tag="$tag" ind1=" " ind2=" ">.*?<subfield code="$sub">(\d+)</;
35     if ($conf->{mode} eq "exclude") {
36         print $S $_ if ($id{$1});
37         print $I $_ unless ($id{$1});;
38     } else {
39         print $S $_ unless ($id{$1});
40         print $I $_ if ($id{$1});;
41     }
42     $conf->{count}++;
43
44     unless ($conf->{count} % 100) {
45         print STDERR "\rProcessed: ",$conf->{count};
46     }
47 }
48
49 =head2 initialize
50
51 Performs boring script initialization. Handles argument parsing,
52 mostly.
53
54 =cut
55
56 sub initialize {
57     my ($c) = @_;
58     my @missing = ();
59
60     # set mode on existing filehandles
61     binmode(STDIN, ':utf8');
62
63     my $rc = GetOptions( $c,
64                          'mode=s',
65                          'include',
66                          'tag|t=i',
67                          'subfield|s=s',
68                          'idfile|i=s',
69                          'marcfile|m=s',
70                          'output-import|oi=s',
71                          'output-shelved|os=s',
72                          'help|h',
73                        );
74     show_help() unless $rc;
75     show_help() if ($c->{help});
76
77     my @keys = keys %{$c};
78     for my $key ('mode', 'idfile', 'marcfile', 'tag', 'subfield',
79                  'output-import', 'output-shelved')
80       { push @missing, $key unless $c->{$key} }
81     if (@missing) {
82         print "Required option: ", join(', ', @missing), " missing!\n";
83         show_help();
84     }
85     unless ($c->{mode} eq "include" or $c->{mode} eq "exclude") {
86         print "Unknown run mode '", $c->{mode}, "'\n";
87         show_help();
88     }
89 }
90
91
92 =head2 show_help
93
94 Display usage message when things go wrong
95
96 =cut
97
98 sub show_help {
99 print <<HELP;
100
101 Usage is: $0 [ARGS]
102
103   --mode=MODE           Runmode to use (exclude, include; usually exclude)
104   --idfile         -i   File of record ids to use as source for matchpoints
105   --marcfile       -m   MARCXML source file
106   --tag            -t   MARC tag to use as matchpoint (903 when matching
107                         incoming records, 901 when matching incumbents)
108   --subfield       -s   Subfield of tag to use ('a' for incoming, 'c'
109                         for incumbent)
110   --output-import  -oi  Output MARCXML file for records to be imported
111   --output-shelved -os  Output MARCXML file for records to be ignored
112
113 If '--mode=exclude' is specified, the record ids in the file specified
114 by -idfile will be used as EXCLUSION data.
115
116 That is, the given record ids will be treated as records which match
117 incumbent records, are being compressed into existing data, and so
118 WILL NOT be imported. The --output-import file will contain records
119 whose ids DO NOT occur in --idfile; --output-shelve will contain the
120 records which DO occur.
121
122 If '--mode=include' is specified, the reverse occurs.
123 HELP
124 exit 1;
125 }