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>
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 (which can be ignored)?
33 # If this isn't the start of the new record, it's a new line in the present record.
36 # Is this line the beginning of a block of data (typically an address or a note)?
37 if ( /^\.(.*?)_BEGIN.$/ ) {
38 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
43 # Is this line the beginning of a block of data (typically an address or a note)?
44 if ( /^\.(.*?)_END.$/ ) {
45 if ("$1." ne $section) {
46 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";
48 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
53 # Looks like we've got some actual data! Let's store it.
54 # FIXME: For large batches of data, we may run out of memory and should store this on disk.
55 if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
57 # Build the name of this field (taking note of whether we're in a named section of data)
64 # Store the field as a key of an array. If it already exists, oh well, now it still exists.
65 $unique_fields{$field} = 1;
67 # Now we can actually store this line of data!
68 $records[$serial]{$field} = $3;
70 # print STDERR "Data extracted: \$records[$serial]{'$field'} = '$3'\n";
75 # This is the continuation of the previous line.
78 $records[$serial]{$field} .= ' ' . $_;
79 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
84 print STDERR "Loaded " . scalar(@records) . " records.\n";
89 @sorted_fields = sort keys %unique_fields;
90 foreach $i (@sorted_fields) {
97 for (my $u = 0; $u < @records; $u++) {
99 foreach $f (@sorted_fields) {
100 if (defined $records[$u]{$f}) {
101 print $records[$u]{$f};
108 print STDERR "Wrote " . scalar(@records) . " records.\n";
109 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS