miker_filter integration work finally proceeding
[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 open $I, '>:utf8', $conf->{'output-import'};
25 open $S, '>:utf8', $conf->{'output-shelve'};
26
27 while (<$M>) {
28     my $tag = $conf->{tag};
29     my $sub = $conf->{subfield};
30
31     /tag="$tag" ind1=" " ind2=" ">.*?<subfield code="$sub">(\d+)</;
32     if ($conf->{incoming}) {
33         print $S $_ if ($id{$1});
34         print $I $_ unless ($id{$1});;
35     } else {
36         print $S $_ unless ($id{$1});
37         print $I $_ if ($id{$1});;
38     }
39     $conf->{count}++;
40
41     unless ($conf->{count} % 100) {
42         print STDERR "\rProcessed: ",$conf->{count};
43     }
44 }
45
46 =head2 initialize
47
48 Performs boring script initialization. Handles argument parsing,
49 mostly.
50
51 =cut
52
53 sub initialize {
54     my ($c) = @_;
55     my @missing = ();
56
57     # set mode on existing filehandles
58     binmode(STDIN, ':utf8');
59
60     my $rc = GetOptions( $c,
61                          'incoming',
62                          'incumbent',
63                          'tag|t=i',
64                          'subfield|s=s',
65                          'idfile|i=s',
66                          'marcfile|m=s',
67                          'outputimport|oi=s',
68                          'outputshelved|os=s',
69                          'help|h',
70                        );
71     show_help() unless $rc;
72     show_help() if ($c->{help});
73
74     $c->{'incoming-tag'}         = 903;
75     $c->{'incoming-subfield'}    = 'a';
76     $c->{'incumbent-tag'}         = 901;
77     $c->{'incumbent-subfield'}    = 'c';
78     my @keys = keys %{$c};
79     unless ($c->{incoming} or $c->{incumbent}) {
80         print "One of --incoming or --incumbent is required.\n";
81         show_help();
82     }
83     if ($c->{incoming} and $c->{incumbent}) {
84         print "Only one of --incoming or --incumbent can be specified.\n";
85         show_help();
86     }
87     for my $key ('idfile', 'marcfile', 'output-import', 'output-shelved')
88       { push @missing, $key unless $c->{$key} }
89     if (@missing) {
90         print "Required option: ", join(', ', @missing), " missing!\n";
91         show_help();
92     }
93
94 }
95
96
97 =head2 show_help
98
99 Display usage message when things go wrong
100
101 =cut
102
103 sub show_help {
104 print <<HELP;
105
106 The purpose of this utility is to split a MARCXML file in twain,
107 producing a set of records which will imported into Evergreen, and a
108 set of records which will not.
109
110 Usage is: $0 [REQUIRED ARGS]
111 Req'd Arguments
112   --incoming  \\___ One (and only one) of these two must
113   --incumbent /    be specified
114
115   If --incoming is specified, the record ids in the file specified by
116   --idfile will be used as EXCLUSION data. That is, the given record
117   ids will be treated as records which match incumbent records and are
118   being compressed into existing data, and so WILL NOT be
119   imported. The --output-import file will contain records whose ids DO
120   NOT occur in --idfile; --output-shelve will contain the records
121   which DO occur.
122
123   If --incumbent is specified, the reverse occurs.
124
125   --idfile         -i   File of record ids to use as source for matchpoints
126   --marcfile       -m   MARCXML source file
127   --output-import  -oi  Output MARCXML file for records to be imported
128   --output-shelve  -os  Output MARCXML file for records to be ignored
129
130 Optional Arguments
131   --tag            -t   MARC tag to use as matchpoint (default 903 for incoming,
132                         901 for incumbent)
133   --subfield       -s   Subfield of tag to use (default 'c' for incoming, 'a'
134                         for incumbent)
135 HELP
136 exit 1;
137 }