On Friday, February 11, 2005 8:58 AM, Mark Bruffey wrote: 
> I need a simple script that will copy existing data from field 852 $6 to
field 949 $a. It this
>possible with MARC::Record?

#The code below is written for those without command-line access
#It will not work as written if you have other subfields you want in the
same 949 field, but those are easy enough to add.
#See MARC::Doc::Tutorial for more information.
#Example U1 covers appending a field.

#The code below only accounts for a single 852 field, and should take the
first if there are multiple.

#!perl
use strict;
use warnings;
use MARC::Batch;
#################
### File Handling ###
#################
#prompt for input file
print ("What is the input file? ");
my $inputfile=<>;
chomp $inputfile;
$inputfile =~ s/^\"(.*)\"$/$1/;
print ("What is the export file? ");
my $exportfile = <>;
chomp $exportfile;
$exportfile =~ s/^\"(.*)\"$/$1/;
#protect against overwriting input file
if ($inputfile =~ /^\Q$exportfile\E$/i) {
    print "Input file and export file are identical.\nProgram will exit now.
Press Enter to continue\n";
    <>;
    die;
}
#check to see if export file exists
if (-f $exportfile) {
    print "That file, $exportfile exists already.\nOverwrite? ";
    my $continue = <>;
    chomp $continue;
    unless ($continue =~ /^y(es)?$/i) {
        #exit program if user typed anything other than y or yes (in any
cap)
        print "Exiting (press Enter)\n"; <>; die;
    }
}

open(OUT, ">$exportfile") or die "Can not open $exportfile, $!";
#initialize $batch as new MARC::Batch object
my $batch = MARC::Batch->new('USMARC', "$inputfile");
############# End File Handling ##########################
#### Start while loop through records in file #####
while (my $record = $batch->next()) {
    #get field 852 subfield 6 data
    my $f852_6 = $record->subfield('852', '6') || ''; #I'm not sure about
the conditional at the end
    $record->append_fields(
        MARC::Field->new('949','','',a=>$f852_6)
    ) if ($f852_6);
    print OUT $record->as_usmarc();

} # while

close $inputfile;
close OUT;

############################################
#the above is untested code. It may contain bugs.

I hope this helps,

Bryan Baldus
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://home.inwave.com/eija
 

Reply via email to