select marc and create one text file per selection
authorJason Etheridge <jason@esilibrary.com>
Thu, 7 Aug 2008 18:27:14 +0000 (18:27 +0000)
committerJason Etheridge <jason@esilibrary.com>
Thu, 7 Aug 2008 18:27:14 +0000 (18:27 +0000)
select_marc_as_text.pl [new file with mode: 0755]

diff --git a/select_marc_as_text.pl b/select_marc_as_text.pl
new file mode 100755 (executable)
index 0000000..955edd9
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use open ':utf8';
+use MARC::Batch;
+use MARC::Record;
+use MARC::File::XML ( BinaryEncoding => 'utf-8' );
+use MARC::Field;
+
+my $record_id_file = $ARGV[0];
+my %record_ids;
+
+open FILE, $record_id_file;
+while (my $record_id = <FILE>) {
+    chomp($record_id); $record_ids{ $record_id } = 1;
+}
+close FILE;
+
+my $id_tag = $ARGV[1]; my $id_subfield = $ARGV[2];
+
+binmode(STDOUT, ':utf8');
+binmode(STDIN, ':utf8');
+
+my $M;
+
+foreach $argnum ( 3 .. $#ARGV ) {
+
+       print STDERR "Processing " . $ARGV[$argnum] . "\n";
+
+    open $M, '<:utf8', $ARGV[$argnum];
+
+       my $batch = MARC::Batch->new('XML',$M);
+       $batch->strict_off();
+       $batch->warnings_off();
+
+    my $count = 0;
+
+       while ( my $record = $batch->next() ) {
+
+        $count++;
+
+               my $id = $record->field($id_tag);
+               if (!$id) {
+                       print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
+                       next;
+               }
+               $id = $id->as_string($id_subfield);
+
+        if (defined $record_ids{ $id }) {
+            open FILE, ">$id.txt";
+            binmode(FILE, ':utf8');
+            print FILE $record->as_formatted();
+            close FILE;
+        }
+       }
+    print STDERR "Processed $count records.\n";
+}