| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head2 NAME | ||
| 4 | |||
| 5 | 003 cleanup script | ||
| 6 | |||
| 7 | =head2 Description | ||
| 8 | |||
| 9 | Runs through each record in file. | ||
| 10 | Compares 001 and 003 fields. | ||
| 11 | Cleans mismatched 003s and outputs to MARC-format file. | ||
| 12 | Reports unmatched 003s. | ||
| 13 | |||
| 14 | =head2 LIMITATIONS | ||
| 15 | |||
| 16 | Currently designed to match only DLC, IOrQBI control numbers. | ||
| 17 | Change those strings to match other needs or add new. | ||
| 18 | |||
| 19 | =head2 TO DO | ||
| 20 | |||
| 21 | Currently only looks for basic match, ignoring trailing spaces. | ||
| 22 | Possible future change, remove extra trailing spaces, making 3-4 spaces after all DLC (workaround for cataloging software problem). | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | ######################## | ||
| 27 | ### Program template ### | ||
| 28 | ######################## | ||
| 29 | ########################### | ||
| 30 | ### Initialize includes ### | ||
| 31 | ### and basic needs ### | ||
| 32 | ########################### | ||
| 33 | use MARC::Batch; | ||
| 34 | use MARC::BBMARC; | ||
| 35 | ##Time coding to wrap around program to determine how long execution takes: | ||
| 36 | |||
| 37 | ########################## | ||
| 38 | ## Time coding routines ## | ||
| 39 | ## Print start time and ## | ||
| 40 | ## set start variable ## | ||
| 41 | ########################## | ||
| 42 | |||
| 43 | use Time::HiRes qw( tv_interval ); | ||
| 44 | # measure elapsed time | ||
| 45 | my $t0 = [Time::HiRes::time()]; | ||
| 46 | my $startingtime = MARC::BBMARC::startstop_time(); | ||
| 47 | # do bunch of stuff here | ||
| 48 | ######################### | ||
| 49 | ### Start main program ## | ||
| 50 | ######################### | ||
| 51 | print ("Welcome to 003 cleanup script\n"); | ||
| 52 | |||
| 53 | ##### File handling initialization ###### | ||
| 54 | #prompt for updated file | ||
| 55 | print ("What is the input file?: "); | ||
| 56 | my $inputfile=<>; | ||
| 57 | chomp $inputfile; | ||
| 58 | $inputfile =~ s/^\"(.*)\"$/$1/; | ||
| 59 | #initialize $infile as new MARC::Batch object | ||
| 60 | my $batch = MARC::Batch->new('USMARC', "$inputfile"); | ||
| 61 | |||
| 62 | print ("Export file for cleaned records\n"); | ||
| 63 | print ("Export record file: "); | ||
| 64 | #read command line for name of export file | ||
| 65 | my $cleanfile= <>; | ||
| 66 | chomp $cleanfile; | ||
| 67 | $cleanfile =~ s/^\"(.*)\"$/$1/; | ||
| 68 | open(CLEANOUT, ">$cleanfile") or die "cannot open Cleanout\n"; | ||
| 69 | |||
| 70 | #if using MacPerl, set creator and type to BBEdit and Text | ||
| 71 | if ($^O eq 'MacOS') { | ||
| 72 | MacPerl::SetFileInfo('R*ch', 'TEXT', $cleanfile); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | ############################################ | ||
| 77 | # Set start time for main calculation loop # | ||
| 78 | ############################################ | ||
| 79 | my $t1 = [Time::HiRes::time()]; | ||
| 80 | my $runningrecordcount=0; | ||
| 81 | ############################################ | ||
| 82 | |||
| 83 | my $fieldscleaned = 0; | ||
| 84 | my $cleanedreccount = 0; | ||
| 85 | |||
| 86 | #loop through batch file of records | ||
| 87 | while (my $record = $batch->next()) { | ||
| 88 | #$reccleaned will be true if 003 is replaced | ||
| 89 | my $reccleaned = 0; | ||
| 90 | |||
| 91 | #get controlno | ||
| 92 | my $controlno = $record->field('001')->as_string(); | ||
| 93 | my $origfield; | ||
| 94 | my $field003; | ||
| 95 | my $before; | ||
| 96 | |||
| 97 | if ($record->field('003')) { | ||
| 98 | $origfield = $record->field('003'); | ||
| 99 | #get 003 field | ||
| 100 | $field003 = $origfield->as_string();} | ||
| 101 | #003 did not exist so create placeholder | ||
| 102 | else { | ||
| 103 | foreach ($record->fields()) { | ||
| 104 | $before = $_; | ||
| 105 | last if ($_->tag() > 003); } | ||
| 106 | $origfield = MARC::Field->new( '003', ' ' ); | ||
| 107 | $field003 = $origfield->as_string(); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | my $cleaned003data; | ||
| 112 | |||
| 113 | #controlno starts with 'a' or spaces then it is LCCN | ||
| 114 | if ($controlno =~ /^[a\s]\s+\d{8}/) {$cleaned003data = 'DLC ';} | ||
| 115 | #controlno starts with 'qbi' then it is QBI | ||
| 116 | elsif ($controlno =~ /^qbi\d{8}/) {$cleaned003data = 'IOrQBI';} | ||
| 117 | ### Add more here ### | ||
| 118 | #skip record if the controlno is not one of the above. | ||
| 119 | else {print "$controlno needs another case\n"; | ||
| 120 | ##################################### | ||
| 121 | ## Place the following within loop ## | ||
| 122 | ##################################### | ||
| 123 | $runningrecordcount++; | ||
| 124 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 125 | ##################################### | ||
| 126 | |||
| 127 | next;} | ||
| 128 | |||
| 129 | #normalize $field003 by removing trailing spaces | ||
| 130 | $field003 =~ s/\s*$//; | ||
| 131 | #create temp var to compare with $field003, removing trailing spaces from $cleaned003data | ||
| 132 | my $cleaned003 = $cleaned003data; | ||
| 133 | $cleaned003 =~ s/\s*$//; | ||
| 134 | |||
| 135 | #ignore good 003 field | ||
| 136 | if ($field003 eq $cleaned003) { | ||
| 137 | |||
| 138 | ##################################### | ||
| 139 | ## Place the following within loop ## | ||
| 140 | ##################################### | ||
| 141 | $runningrecordcount++; | ||
| 142 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 143 | ##################################### | ||
| 144 | |||
| 145 | next;} | ||
| 146 | |||
| 147 | #add new 003 (did not exist before) with $cleaned003data | ||
| 148 | elsif ($before) { | ||
| 149 | my $newfield = MARC::Field->new('003', $cleaned003data); | ||
| 150 | $record->insert_fields_before($before, $newfield); | ||
| 151 | $reccleaned = 1; | ||
| 152 | $fieldscleaned++; | ||
| 153 | } #elsif | ||
| 154 | |||
| 155 | #replace bad 003 field (that existed) with $cleaned003data | ||
| 156 | else { | ||
| 157 | my $newfield = MARC::Field->new('003', $cleaned003data); | ||
| 158 | $origfield->replace_with($newfield); | ||
| 159 | $reccleaned = 1; | ||
| 160 | $fieldscleaned++; | ||
| 161 | } #else | ||
| 162 | |||
| 163 | |||
| 164 | #print cleaned record if it has changed | ||
| 165 | if ($reccleaned) {print CLEANOUT ($record->as_usmarc()); | ||
| 166 | $cleanedreccount++;} | ||
| 167 | |||
| 168 | ##################################### | ||
| 169 | ## Place the following within loop ## | ||
| 170 | ##################################### | ||
| 171 | $runningrecordcount++; | ||
| 172 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 173 | ##################################### | ||
| 174 | } #while records are in infile | ||
| 175 | |||
| 176 | print "$fieldscleaned fields were cleaned in $cleanedreccount records\n"; | ||
| 177 | |||
| 178 | ########################## | ||
| 179 | ### Main program done. ## | ||
| 180 | ### Report elapsed time.## | ||
| 181 | ########################## | ||
| 182 | my $elapsed = tv_interval ($t0); | ||
| 183 | my $calcelapsed = tv_interval ($t1); | ||
| 184 | print sprintf ("%.4f %s\n", "$elapsed", "seconds from execution\n"); | ||
| 185 | print sprintf ("%.4f %s\n", "$calcelapsed", "seconds to calculate\n"); | ||
| 186 | my $endingtime = MARC::BBMARC::startstop_time(); | ||
| 187 | print "Started at $startingtime\nEnded at $endingtime"; | ||
| 188 | print "Press Enter to quit"; | ||
| 189 | <>; | ||
| 190 | |||
| 191 | ##################### | ||
| 192 | ### END OF PROGRAM ## | ||
| 193 | ##################### | ||
| 194 | =head1 LICENSE | ||
| 195 | |||
| 196 | This code may be distributed under the same terms as Perl itself. | ||
| 197 | |||
| 198 | Please note that this code is not a product of or supported by the | ||
| 199 | employers of the various contributors to the code. | ||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | Bryan Baldus | ||
| 204 | eija [at] inwave [dot] com | ||
| 205 | |||
| 206 | Copyright (c) 2003-2004 | ||
| 207 | |||
| 208 | =cut |