1 |
dpavlin |
1316 |
#!/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 |
dpavlin |
1319 |
my $debug = 0; |
13 |
dpavlin |
1316 |
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 |
dpavlin |
1319 |
warn dump( @fields ) if $debug; |
43 |
dpavlin |
1316 |
|
44 |
|
|
my $marc = new MARC::Record; |
45 |
|
|
$marc->encoding( 'utf-8' ); |
46 |
|
|
$marc->leader( $leader ); |
47 |
|
|
$marc->add_fields( @fields ); |
48 |
|
|
|
49 |
dpavlin |
1319 |
warn $marc->as_formatted, $/, $/; |
50 |
dpavlin |
1316 |
print $marc->as_usmarc; |
51 |
|
|
|
52 |
|
|
@fields = (); |
53 |
|
|
} else { |
54 |
dpavlin |
1319 |
warn "IGNORED: $_\n" if $debug; |
55 |
dpavlin |
1316 |
} |
56 |
|
|
|
57 |
|
|
} |