#!/usr/local/bin/perl # oclc980.pl # desc: split oclc promptcat file into invoice files based on 980 |f fields # wordsettings: # end_autoz use strict; if ($#ARGV < 0) {usage();} my $marcfile = $ARGV[0]; my ($mctr, $fopen, $plural, $strptr, $marcrec); my ($bibbaseaddr, $bibtaglen, $biboffset); my @bibtagid = (); my @bibtagdata = (); my @chunk = (); my %invhash = (); my %allinv = (); my $subfdelim = chr(0x1f); my $fdelim = chr(0x1e); my $recdelim = chr(0x1d); my ($idx, $thisinvnum, $tag980, $outfile); my $filext = 'marc'; # set up MARC end-of-record character; needed for reading the MARC file $/ = chr(0x1d); $fopen = sprintf("Cannot open file %s for input\n", $marcfile); open(marcfile, $marcfile) or die $fopen; $mctr = 0; while ($marcrec = ) { $bibbaseaddr = substr($marcrec, 12, 5) - 1; $strptr = 24; $idx = 0; while ($strptr < $bibbaseaddr-1) { $bibtagid[$idx] = substr($marcrec, $strptr, 3); if ($bibtagid[$idx] eq '980') {$tag980 = $idx;} $bibtaglen = substr($marcrec, $strptr+3, 4); $biboffset = substr($marcrec, $strptr+7, 5); $bibtagdata[$idx] = substr($marcrec, $bibbaseaddr+$biboffset, $bibtaglen); $strptr+= 12; $idx++; } @chunk = split /\x1f/, $bibtagdata[$tag980]; $idx = 0; while ($idx < @chunk) { if (substr($chunk[$idx], 0, 1) eq 'f') { $thisinvnum = substr($chunk[$idx], 1); if (!exists($invhash{$thisinvnum})) { $invhash{$thisinvnum} = 1; $allinv{$thisinvnum} = $thisinvnum; } else {$invhash{$thisinvnum}++;} $outfile = "$thisinvnum.marc"; $fopen = sprintf("Cannot open %s for append output\n", $outfile); open(outfile, ">>$outfile") or die $fopen; print outfile $marcrec; close(outfile); } $idx++; } $mctr++; } close(marcfile); print "\n"; print "$mctr records read\n"; print "Output is...\n"; foreach $thisinvnum (sort keys %allinv) { print "Invoice # $thisinvnum has $invhash{$thisinvnum} record(s) in file $thisinvnum.$filext\n"; } sub usage() { printf ("\nUsage: oclc980.pl promptcatfile\n"); printf (" where promptcatfile is that file from OCLC.\n"); printf (" An output file will be generated for each unique 980 |f.\n"); printf (" Records with 980 |f fields matching the file name will be\n"); printf (" put in that file.\n"); exit(0); }