b0da24519501594675c42666a2dc6b1863f1c786
[migration-tools.git] / spit_csv.pl
1 #!/usr/bin/perl
2 use MARC::Batch;
3 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
4 use MARC::Field;
5 use Unicode::Normalize;
6
7
8 my @desired_tags_subfields = ();
9 foreach my $argnum ( 1 .. $#ARGV) {
10     push @desired_tags_subfields, $ARGV[$argnum];
11 }
12
13 my $count = 0;
14
15 binmode(STDOUT, ':utf8');
16 binmode(STDIN, ':utf8');
17
18 foreach my $argnum ( 0 .. 0 ) {
19
20         print STDERR "Processing " . $ARGV[$argnum] . "\n";
21
22         my $batch = MARC::Batch->new('XML',$ARGV[$argnum]);
23         $batch->strict_off();
24         $batch->warnings_off();
25
26         while ( my $record = $batch->next() ) {
27
28         $count++;
29
30                 print STDERR "WARNINGS: Record $count : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
31
32         for (my $i = 0; $i < scalar(@desired_tags_subfields); $i+=2) {
33                     my @tags; if ($record->field($desired_tags_subfields[$i])) { @tags = $record->field($desired_tags_subfields[$i]); }
34             foreach my $f ( @tags ) { 
35                 if ($f->subfield($desired_tags_subfields[$i+1])) { 
36                     print STDOUT $f->subfield($desired_tags_subfields[$i+1]) . "\t";
37                 } 
38             }
39         }
40         print STDOUT "\n";
41
42         }
43         print STDERR "Processed $count records\n";
44 }