1 |
#!/usr/bin/perl |
2 |
|
3 |
# Convert marcdump output back to marc file |
4 |
|
5 |
use warnings; |
6 |
use strict; |
7 |
|
8 |
use MARC::Record; |
9 |
use Data::Dump qw(dump); |
10 |
use Encode; |
11 |
|
12 |
my $debug = 0; |
13 |
my @fields; |
14 |
|
15 |
sub parse_subfields { |
16 |
my $text = shift; |
17 |
if ( $text =~ /^_([a-z])(.+)$/ ) { |
18 |
return ( $1, $2 ); |
19 |
} else { |
20 |
$text =~ s/^ // || warn "no space in front of: '$text'\n"; |
21 |
return $text; |
22 |
} |
23 |
} |
24 |
|
25 |
my $leader; |
26 |
|
27 |
while(<>) { |
28 |
chomp; |
29 |
$_ = decode('utf-8', $_); |
30 |
|
31 |
if ( /^LDR (.+)$/ ) { |
32 |
$leader = $1; |
33 |
} elsif ( /^(\d\d\d) (.)(.) (.+)$/ ) { |
34 |
if ( $1 < 10 ) { |
35 |
push @fields, [ $1, parse_subfields($4) ]; |
36 |
} else { |
37 |
push @fields, [ $1, $2, $3, parse_subfields($4) ]; |
38 |
} |
39 |
} elsif ( /^\s{7}_([a-z])(.+)$/ ) { |
40 |
push @{ $fields[ $#fields ] }, $1, $2; |
41 |
} elsif ( /^$/ ) { |
42 |
warn dump( @fields ) if $debug; |
43 |
|
44 |
my $marc = new MARC::Record; |
45 |
$marc->encoding( 'utf-8' ); |
46 |
$marc->leader( $leader ); |
47 |
$marc->add_fields( @fields ); |
48 |
|
49 |
warn $marc->as_formatted, $/, $/; |
50 |
print $marc->as_usmarc; |
51 |
|
52 |
@fields = (); |
53 |
} else { |
54 |
warn "IGNORED: $_\n" if $debug; |
55 |
} |
56 |
|
57 |
} |