dd4897c400413d0069bbb051d108b8a0357b7287
[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 -h
31
32 dump result of C<to_hash> on record
33
34 =item -d
35
36 turn debugging output on
37
38 =item -t
39
40 dump tsv file for TokyoCabinet import
41
42 =back
43
44 =cut
45
46 my %opt;
47 getopts('do:l:ht', \%opt);
48
49 my $file = shift @ARGV || die "usage: $0 [-o offset] [-l limit] [-h] [-d] file.marc\n";
50
51 my $marc = new MARC::Fast(
52         marcdb => $file,
53         debug => $opt{d},
54 );
55
56
57 my $min = 1;
58 my $max = $marc->count;
59
60 if (my $mfn = $opt{n}) {
61         $min = $max = $mfn;
62         print STDERR "Dumping $mfn only\n";
63 } elsif (my $limit = $opt{l}) {
64         print STDERR "$file has $max records, using first $limit\n";
65         $max = $limit;
66 } else {
67         print STDERR "$file has $max records...\n";
68 }
69
70 for my $mfn ($min .. $max) {
71         my $rec = $marc->fetch($mfn) || next;
72         warn "rec is ",dump($rec) if ($opt{d});
73         if ( $opt{t} ) {
74                 print "rec\t$mfn\tleader\t", $marc->last_leader, "\t";
75                 my $ascii = $marc->to_ascii($mfn);
76                 $ascii =~ s{\n}{\t}gs;
77                 print "$ascii\n";
78         } else {
79                 print "REC $mfn\n";
80                 print $marc->last_leader,"\n";
81                 print $marc->to_ascii($mfn),"\n";
82         }
83         warn "hash is ",dump($marc->to_hash($mfn, include_subfields => 1)) if ($opt{h});
84 }