| 1 | #!/usr/local/bin/perl | ||
| 2 | =head2 | ||
| 3 | |||
| 4 | Looks for extra spaces at the end of fields greater than 010. | ||
| 5 | Removes unnecessary spaces. | ||
| 6 | Also ignores all 016 fields. | ||
| 7 | Outputs records that have been cleaned. | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | ########################### | ||
| 12 | ### Initialize includes ### | ||
| 13 | ### and basic needs ### | ||
| 14 | ########################### | ||
| 15 | use strict; | ||
| 16 | use MARC::Batch; | ||
| 17 | use MARC::BBMARC; | ||
| 18 | ########################## | ||
| 19 | ## Time coding routines ## | ||
| 20 | ## Print start time and ## | ||
| 21 | ## set start variable ## | ||
| 22 | ########################## | ||
| 23 | |||
| 24 | use Time::HiRes qw( tv_interval ); | ||
| 25 | # measure elapsed time | ||
| 26 | my $t0 = [Time::HiRes::time()]; | ||
| 27 | my $startingtime = MARC::BBMARC::startstop_time(); | ||
| 28 | ######################### | ||
| 29 | ### Start main program ## | ||
| 30 | ######################### | ||
| 31 | |||
| 32 | print ("Welcome to trailing spaces cleanup\n"); | ||
| 33 | |||
| 34 | ##### File handling initialization ###### | ||
| 35 | #prompt for input file | ||
| 36 | print ("What is the input file? "); | ||
| 37 | my $inputfile=<>; | ||
| 38 | chomp $inputfile; | ||
| 39 | $inputfile =~ s/^\"(.*)\"$/$1/; | ||
| 40 | print ("What is the export file? "); | ||
| 41 | my $exportfile = <>; | ||
| 42 | chomp $exportfile; | ||
| 43 | $exportfile =~ s/^\"(.*)\"$/$1/; | ||
| 44 | open(OUT, ">$exportfile"); | ||
| 45 | #if using MacPerl, set creator and type to BBEdit and Text | ||
| 46 | if ($^O eq 'MacOS') { | ||
| 47 | MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | #initialize $infile as new usmarc file object | ||
| 53 | my $batch = MARC::Batch->new('USMARC', "$inputfile"); | ||
| 54 | ########## Start extraction ######### | ||
| 55 | |||
| 56 | ############################################ | ||
| 57 | # Set start time for main calculation loop # | ||
| 58 | ############################################ | ||
| 59 | my $t1 = [Time::HiRes::time()]; | ||
| 60 | my $runningrecordcount=0; | ||
| 61 | ################################################### | ||
| 62 | #initialize counting and notification variables | ||
| 63 | my $fieldcleanedcount = 0; | ||
| 64 | my $cleanedreccount=0; | ||
| 65 | #### Start while loop through records in file ##### | ||
| 66 | while (my $record = $batch->next()) { | ||
| 67 | #new record so reset $recordchanged | ||
| 68 | my $recordchanged = 0; | ||
| 69 | |||
| 70 | #look at each field in record | ||
| 71 | foreach my $field ($record->fields()) { | ||
| 72 | #skip control fields and LCCN (010) | ||
| 73 | next if ($field->tag()<=10); | ||
| 74 | next if ($field->tag() == 16); | ||
| 75 | #create array holding arrayrefs for subfield code and data | ||
| 76 | my @subfields= $field->subfields(); | ||
| 77 | |||
| 78 | #look at data in last subfield | ||
| 79 | my $lastsubfield = pop (@subfields); | ||
| 80 | |||
| 81 | #each $subfield is an array ref containing a subfield code character and subfield data | ||
| 82 | my ($code, $data) = @$lastsubfield; | ||
| 83 | |||
| 84 | #look for one or more instances of spaces at end of subfield data | ||
| 85 | if ($data =~ /\s+$/) { | ||
| 86 | #field had extra spaces | ||
| 87 | #declare array to store subfields after cleaning the last one | ||
| 88 | my @newSubfields = (); | ||
| 89 | |||
| 90 | #remove all extra white space at end of data | ||
| 91 | $data =~ s/\s*$//; | ||
| 92 | #put last subfield onto newSubfields array | ||
| 93 | unshift (@newSubfields, $code, $data); | ||
| 94 | |||
| 95 | #put the rest of the subfields onto @newSubfields | ||
| 96 | while (my $subfield = pop (@subfields)) { | ||
| 97 | my ($code, $data) = @$subfield; | ||
| 98 | unshift (@newSubfields, $code, $data); | ||
| 99 | } | ||
| 100 | |||
| 101 | $fieldcleanedcount++; | ||
| 102 | #replace field in $record | ||
| 103 | my $newfield = MARC::Field->new ( | ||
| 104 | $field->tag(), | ||
| 105 | $field->indicator(1), | ||
| 106 | $field->indicator(2), | ||
| 107 | @newSubfields | ||
| 108 | ); | ||
| 109 | |||
| 110 | $field->replace_with($newfield); | ||
| 111 | $recordchanged = 1; | ||
| 112 | |||
| 113 | } #if had spaces | ||
| 114 | } # foreach field | ||
| 115 | |||
| 116 | if ($recordchanged) {print OUT $record->as_usmarc; | ||
| 117 | $cleanedreccount++; | ||
| 118 | } | ||
| 119 | ################################################### | ||
| 120 | ### add to count for user notification ### | ||
| 121 | $runningrecordcount++; | ||
| 122 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 123 | ################################################### | ||
| 124 | } # while | ||
| 125 | |||
| 126 | close $inputfile; | ||
| 127 | close OUT; | ||
| 128 | print "$fieldcleanedcount fields cleaned\n"; | ||
| 129 | print "$cleanedreccount records cleaned in $runningrecordcount records scanned\n"; | ||
| 130 | |||
| 131 | ########################## | ||
| 132 | ### Main program done. ## | ||
| 133 | ### Report elapsed time.## | ||
| 134 | ########################## | ||
| 135 | |||
| 136 | my $elapsed = tv_interval ($t0); | ||
| 137 | my $calcelapsed = tv_interval ($t1); | ||
| 138 | print sprintf ("%.4f %s\n", "$elapsed", "seconds from execution\n"); | ||
| 139 | print sprintf ("%.4f %s\n", "$calcelapsed", "seconds to calculate\n"); | ||
| 140 | my $endingtime = MARC::BBMARC::startstop_time(); | ||
| 141 | print "Started at $startingtime\nEnded at $endingtime"; | ||
| 142 | |||
| 143 | print "\n\nPress Enter to quit"; | ||
| 144 | <>; | ||
| 145 | |||
| 146 | ##################### | ||
| 147 | ### END OF PROGRAM ## | ||
| 148 | ##################### | ||
| 149 | =head1 LICENSE | ||
| 150 | |||
| 151 | This code may be distributed under the same terms as Perl itself. | ||
| 152 | |||
| 153 | Please note that this code is not a product of or supported by the | ||
| 154 | employers of the various contributors to the code. | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Bryan Baldus | ||
| 159 | eija [at] inwave [dot] com | ||
| 160 | |||
| 161 | Copyright (c) 2003-2004 | ||
| 162 | |||
| 163 | =cut |