3 # Converts a Unicorn users.data, bill.data, or charge.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
12 my @output_fields = qw( l_form l_user_id l_item_id l_billing_lib l_bill_date l_bill_amt l_bill_reason ); # in that order
17 # print STDERR "Loaded this line: " . $_;
19 # Is this the start of a new record?
20 if ( /^... DOCUMENT BOUNDARY ...$/ ) {
23 $section = ''; # just in case this didn't get reset in the previous record
24 # print STDERR "Processing record $serial.\n";
28 # Is this a FORM= line?
30 $records[$serial]{'l_form'} = $1;
34 # If this isn't the start of the new record, it's a new line in the present record.
37 # Is this line the beginning of a block of data (typically an address or a note)?
38 if ( /^\.(.*?)_BEGIN.$/ ) {
39 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
44 # Is this line the beginning of a block of data (typically an address or a note)?
45 if ( /^\.(.*?)_END.$/ ) {
46 if ("$1." ne $section) {
47 print STDERR "Error in record $serial, line $line (input line $.): got an end-of-$1 but I thought I was in a $section block!\n";
49 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
54 # Looks like we've got some actual data! Let's store it.
55 # FIXME: For large batches of data, we may run out of memory and should store this on disk.
56 if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
58 # Build the name of this field (taking note of whether we're in a named section of data)
65 # Now we can actually store this line of data!
67 if ($field eq 'USER_ID') { $records[$serial]{'l_user_id'} = $3; }
68 if ($field eq 'ITEM_ID') { $records[$serial]{'l_item_id'} = $3; }
69 if ($field eq 'BILL_DB') { $records[$serial]{'l_bill_date'} = $3; }
70 if ($field eq 'BILL_AMOUNT') { $records[$serial]{'l_bill_amt'} = $3; }
71 if ($field eq 'BILL_LIBRARY') { $records[$serial]{'l_billing_lib'} = $3; }
72 if ($field eq 'BILL_REASON') { $records[$serial]{'l_bill_reason'} = $3; }
77 # This is the continuation of the previous line.
80 $records[$serial]{$field} .= ' ' . $_;
81 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
86 print STDERR "Loaded " . scalar(@records) . " records.\n";
89 foreach $i (@output_fields) {
96 for (my $u = 0; $u < @records; $u++) {
97 foreach $f (@output_fields) {
98 if (defined $records[$u]{$f}) {
99 print $records[$u]{$f};
106 print STDERR "Wrote " . scalar(@records) . " records.\n";
107 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS