fix regex for extracting bib ID from input
[migration-tools.git] / extract_loadset
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use open ':utf8';
5
6 use Getopt::Long;
7
8 my $conf  = {}; # configuration hashref
9 initialize($conf);
10
11 # build exclusion hash
12 open FP, '<', shift or die "Can't open matchset file: $!\n";
13 my %exclude = ();
14 while (<FP>) {
15     chomp;
16     my ($lead,$sub) = split /\t/;
17     $sub =~ s/\s//g; # any whitespace is extraneous
18     $exclude{$sub}  = 1 unless ($sub < $conf->{lowerbound});
19 }
20 close FP;
21
22 # strip exclusions from marcxml file
23 open MI, '<', $conf->{input} or die "Can't open input file: $!\n";
24 open MO, '>', $conf->{output} or die "Can't open output file: $!\n";
25 while (<MI>) {
26     m/tag="$conf->{tag}".+?<subfield code="$conf->{subfield}">(\d+)</;
27     print MO unless $exclude{$1};
28 }
29
30
31 sub initialize {
32     my ($c) = @_;
33     my @missing = ();
34
35     # set mode on existing filehandles
36     binmode(STDIN, ':utf8');
37
38     my $rc = GetOptions( $c,
39                          'lowerbound|l=i',
40                          'input|i=s',
41                          'output|o=s',
42                          'tag|t=i',
43                          'subfield|s=s',
44                          'help|h',
45                        );
46     show_help() unless $rc;
47     show_help() if ($c->{help});
48
49     $conf->{tag} = $conf->{tag} || 903;
50     $conf->{subfield} = $conf->{subfield} || 'a';
51
52     my @keys = keys %{$c};
53     show_help() unless (@ARGV and @keys);
54     for my $key ('output', 'lowerbound', 'input')
55       { push @missing, $key unless $c->{$key} }
56     if (@missing) {
57         print "Required option: ", join(', ', @missing), " missing!\n";
58         show_help();
59     }
60 }
61
62 sub show_help {
63     print <<HELP;
64 Usage is: extract_loadset -l BOUND -i INPUTXML -o OUTPUTXML MATCHSET
65
66   --lowerbound  -l  Lowest record ID which will be included in the loadset
67   --input       -i  MARCXML input file
68   --output      -o  MARCXML output file
69   --tag         -t  MARC tag to use as identifier (default: 903)
70   --subfield    -s  Subfield of --tag argument (default: 'a')
71 HELP
72 exit;
73 }