| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head2 NAME | ||
| 4 | |||
| 5 | 007 cleanup script | ||
| 6 | |||
| 7 | =head2 Description | ||
| 8 | |||
| 9 | Runs through each record in file. | ||
| 10 | Calls subroutine to check validity of each 007 value. | ||
| 11 | Reports any records needing manual correcting (outputs these to file). Cleans fields with valid values, but that are too long, and outputs these to separate file. | ||
| 12 | |||
| 13 | Both output files are USMARC/MARC21 format. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | ######################## | ||
| 18 | ### Program template ### | ||
| 19 | ######################## | ||
| 20 | ########################### | ||
| 21 | ### Initialize includes ### | ||
| 22 | ### and basic needs ### | ||
| 23 | ########################### | ||
| 24 | use MARC::Batch; | ||
| 25 | use MARC::BBMARC; | ||
| 26 | use MARC::Lintadditions; | ||
| 27 | ##Time coding to wrap around program to determine how long execution takes: | ||
| 28 | |||
| 29 | ########################## | ||
| 30 | ## Time coding routines ## | ||
| 31 | ## Print start time and ## | ||
| 32 | ## set start variable ## | ||
| 33 | ########################## | ||
| 34 | |||
| 35 | use Time::HiRes qw( tv_interval ); | ||
| 36 | # measure elapsed time | ||
| 37 | my $t0 = [Time::HiRes::time()]; | ||
| 38 | my $startingtime = MARC::BBMARC::startstop_time(); | ||
| 39 | # do bunch of stuff here | ||
| 40 | ######################### | ||
| 41 | ### Start main program ## | ||
| 42 | ######################### | ||
| 43 | print ("Welcome to 007 cleanup script\n"); | ||
| 44 | |||
| 45 | ##### File handling initialization ###### | ||
| 46 | #prompt for updated file | ||
| 47 | print ("What is the input file?: "); | ||
| 48 | my $inputfile=<>; | ||
| 49 | chomp $inputfile; | ||
| 50 | $inputfile =~ s/^\"(.*)\"$/$1/; | ||
| 51 | #initialize $infile as new MARC::Batch object | ||
| 52 | my $batch = MARC::Batch->new('USMARC', "$inputfile"); | ||
| 53 | |||
| 54 | print ("Export file for cleaned records\n"); | ||
| 55 | print ("Export record file: "); | ||
| 56 | #read command line for name of export file | ||
| 57 | my $cleanfile= <>; | ||
| 58 | chomp $cleanfile; | ||
| 59 | $cleanfile =~ s/^\"(.*)\"$/$1/; | ||
| 60 | open(CLEANOUT, ">$cleanfile") or die "cannot open Cleanout\n"; | ||
| 61 | if ($^O eq 'MacOS') { | ||
| 62 | #set creator and type to BBEdit and Text | ||
| 63 | MacPerl::SetFileInfo('R*ch', 'TEXT', $cleanfile); | ||
| 64 | } | ||
| 65 | |||
| 66 | print ("Export file for manual check records\n"); | ||
| 67 | print ("Manual check record file: "); | ||
| 68 | my $badfieldfile = <>; | ||
| 69 | chomp $badfieldfile; | ||
| 70 | $badfieldfile =~ s/^\"(.*)\"$/$1/; | ||
| 71 | open(VISUALIZEOUT, ">$badfieldfile") or die "cannot open Badfieldfile\n"; | ||
| 72 | if ($^O eq 'MacOS') { | ||
| 73 | #set creator and type to BBEdit and Text | ||
| 74 | MacPerl::SetFileInfo('R*ch', 'TEXT', $badfieldfile); | ||
| 75 | } | ||
| 76 | |||
| 77 | ############################################ | ||
| 78 | # Set start time for main calculation loop # | ||
| 79 | ############################################ | ||
| 80 | my $t1 = [Time::HiRes::time()]; | ||
| 81 | my $runningrecordcount=0; | ||
| 82 | ############################################ | ||
| 83 | |||
| 84 | my $fieldscleaned = 0; | ||
| 85 | my $badbytecount = 0; | ||
| 86 | my $visualizecount = 0; | ||
| 87 | my $cleanedreccount = 0; | ||
| 88 | |||
| 89 | #loop through batch file of records | ||
| 90 | while (my $record = $batch->next()) { | ||
| 91 | #$reccleaned will be true if 007 is replaced | ||
| 92 | my $reccleaned = 0; | ||
| 93 | #$badrecord will be true if bytes have 'bad' or if data exists after limit | ||
| 94 | my $badrecord; | ||
| 95 | |||
| 96 | #look at each 007 field in record | ||
| 97 | foreach my $field ( $record->field('007') ) { | ||
| 98 | my $field007 = $field->as_string(); | ||
| 99 | my @bytes = (split ('', $field007)); | ||
| 100 | |||
| 101 | #clean byte[2] before passing (change 'u' or '|' to blank space) | ||
| 102 | $bytes[2] =~ s/[u|]/ /; | ||
| 103 | #call validate007 sub, which is part of Lintadditions | ||
| 104 | #The sub returns an arrayref and a scalarref | ||
| 105 | my ($arrayref007, $hasextradataref) = MARC::Lintadditions::validate007(\@bytes); | ||
| 106 | |||
| 107 | #dereference the returned values | ||
| 108 | my @cleaned007 = @$arrayref007; | ||
| 109 | #loop through the array looking for bad bytes | ||
| 110 | for (my $i = 0 ; $i <= $#cleaned007; $i++) { | ||
| 111 | if ($cleaned007[$i] eq 'bad'){ | ||
| 112 | #set marker for printing bad record | ||
| 113 | $badrecord = 1; | ||
| 114 | $badbytecount++; | ||
| 115 | } #if bad byte | ||
| 116 | } #for each byte | ||
| 117 | |||
| 118 | #check for data after valid limit | ||
| 119 | if ($$hasextradataref) { | ||
| 120 | #set marker for printing bad record | ||
| 121 | $badrecord = 1; | ||
| 122 | $visualizecount++; | ||
| 123 | } | ||
| 124 | |||
| 125 | #unless badrecord was found, replace old 007 with new | ||
| 126 | #also ignore 007s that didn't have extra spaces | ||
| 127 | unless ($badrecord) { | ||
| 128 | my $cleaned007data = join ('', @cleaned007); | ||
| 129 | #ignore good 007 fields | ||
| 130 | if ($field007 eq $cleaned007data) {next;} | ||
| 131 | else { | ||
| 132 | my $newfield = MARC::Field->new('007', $cleaned007data); | ||
| 133 | $field->replace_with($newfield); | ||
| 134 | $reccleaned = 1; | ||
| 135 | $fieldscleaned++; | ||
| 136 | } #else | ||
| 137 | } #unless | ||
| 138 | |||
| 139 | } #for each 007 field | ||
| 140 | |||
| 141 | #if bad 007 was found print it out for manual checking | ||
| 142 | if ($badrecord) { | ||
| 143 | print VISUALIZEOUT ($record->as_usmarc()); | ||
| 144 | } | ||
| 145 | #otherwise, print cleaned record | ||
| 146 | elsif ($reccleaned) {print CLEANOUT ($record->as_usmarc()); | ||
| 147 | $cleanedreccount++;} | ||
| 148 | |||
| 149 | ##################################### | ||
| 150 | ## Place the following within loop ## | ||
| 151 | ##################################### | ||
| 152 | $runningrecordcount++; | ||
| 153 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 154 | ##################################### | ||
| 155 | } #while records are in infile | ||
| 156 | |||
| 157 | print "$badbytecount total bytes are bad\n"; | ||
| 158 | print "$visualizecount fields have data after limit\n"; | ||
| 159 | print "$fieldscleaned fields were cleaned in $cleanedreccount records\n"; | ||
| 160 | |||
| 161 | ########################## | ||
| 162 | ### Main program done. ## | ||
| 163 | ### Report elapsed time.## | ||
| 164 | ########################## | ||
| 165 | my $elapsed = tv_interval ($t0); | ||
| 166 | my $calcelapsed = tv_interval ($t1); | ||
| 167 | print sprintf ("%.4f %s\n", "$elapsed", "seconds from execution\n"); | ||
| 168 | print sprintf ("%.4f %s\n", "$calcelapsed", "seconds to calculate\n"); | ||
| 169 | my $endingtime = MARC::BBMARC::startstop_time(); | ||
| 170 | print "Started at $startingtime\nEnded at $endingtime"; | ||
| 171 | print "Press Enter to quit"; | ||
| 172 | <>; | ||
| 173 | |||
| 174 | ##################### | ||
| 175 | ### END OF PROGRAM ## | ||
| 176 | ##################### | ||
| 177 | |||
| 178 | =head1 LICENSE | ||
| 179 | |||
| 180 | This code may be distributed under the same terms as Perl itself. | ||
| 181 | |||
| 182 | Please note that this code is not a product of or supported by the | ||
| 183 | employers of the various contributors to the code. | ||
| 184 | |||
| 185 | =head1 AUTHOR | ||
| 186 | |||
| 187 | Bryan Baldus | ||
| 188 | eija [at] inwave [dot] com | ||
| 189 | |||
| 190 | Copyright (c) 2003-2004 | ||
| 191 | |||
| 192 | =cut |