From 1c92a0dabca329d4a876e90819d8e7be8494140e Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Thu, 7 Aug 2008 18:27:14 +0000 Subject: [PATCH] select marc and create one text file per selection --- select_marc_as_text.pl | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) create mode 100755 select_marc_as_text.pl diff --git a/select_marc_as_text.pl b/select_marc_as_text.pl new file mode 100755 index 0000000..955edd9 --- /dev/null +++ b/select_marc_as_text.pl @@ -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 = ) { + 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"; +} -- 1.7.2.5