a9400070af236b8448028dd2aa11235437bd0523
[MARC-Fast] / scripts / dump_fastmarc.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use MARC::Fast;
7 use Getopt::Std;
8 use Data::Dump qw/dump/;
9
10 =head1 NAME
11
12 dump_fastmarc.pl - display MARC records
13
14 =head2 USAGE
15
16   dump_fastmarc.pl /path/to/dump.marc
17
18 =head2 OPTIONS
19
20 =over 16
21
22 =item -o offset
23
24 dump records starting with C<offset>
25
26 =item -l limit
27
28 dump just C<limit> records
29
30 =item -n mfn
31
32 dump single C<mfn> record (same as C<-o mfn -l 1>)
33
34 =item -h
35
36 dump result of C<to_hash> on record
37
38 =item -d
39
40 turn debugging output on
41
42 =item -t
43
44 dump tsv file for TokyoCabinet import
45
46 =back
47
48 =cut
49
50 my %opt;
51 getopts('do:l:n:ht', \%opt);
52
53 my $file = shift @ARGV || die "usage: $0 [-o offset] [-l limit] [-n single_mfn] [-h] [-d] [-t] file.marc\n";
54
55 my $marc = new MARC::Fast(
56         marcdb => $file,
57         debug => $opt{d},
58 );
59
60 my $min = $opt{o} || 1;
61 my $max = $marc->count;
62
63 if (my $mfn = $opt{n}) {
64         $min = $max = $mfn;
65         print STDERR "Dumping $mfn only\n";
66 } elsif (my $limit = $opt{l}) {
67         print STDERR "$file has $max records, using first $limit\n";
68         $max = $min + $limit - 1;
69 } else {
70         print STDERR "$file has $max records...\n";
71 }
72
73 for my $mfn ($min .. $max) {
74         my $rec = $marc->fetch($mfn) || next;
75         warn "rec is ",dump($rec) if ($opt{d});
76         if ( $opt{t} ) {
77                 print "rec\t$mfn\tleader\t", $marc->last_leader, "\t";
78                 my $ascii = $marc->to_ascii($mfn);
79                 $ascii =~ s{\n}{\t}gs;
80                 print "$ascii\n";
81         } else {
82                 print "REC $mfn\n";
83                 print $marc->last_leader,"\n";
84                 print $marc->to_ascii($mfn),"\n";
85         }
86         warn "hash is ",dump($marc->to_hash($mfn, include_subfields => 1)) if ($opt{h});
87 }