change \r\n to \n if they occur
[migration-tools.git] / unicorn / unicorn_patrons_to_tsv.pl
1 #!/usr/bin/perl -w
2
3 # Converts a Unicorn users.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
5
6 my @users;
7 my $serial = -1;
8 my $line = 0;
9 my $section = '';
10 my $field = '';
11 my %unique_fields;
12
13
14 # Load each record
15 while (<>) {
16     s/\r\n/\n/g;
17 # print STDERR "Loaded this line: " . $_;
18
19         # Is this the start of a new record?
20         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
21                 $line = 0;
22                 $serial++;
23                 $section = ''; # just in case this didn't get reset in the previous record
24                 # print STDERR "Processing record $serial.\n";
25                 next;
26         }
27
28         # If this isn't the start of the new record, it's a new line in the present record.
29         $line++;
30
31         # Is this line the beginning of a block of data (typically an address or a note)?
32         if ( /^\.(.*?)_BEGIN.$/ ) {
33                 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
34                 $section = "$1.";
35                 next;
36         }
37
38         # Is this line the beginning of a block of data (typically an address or a note)?
39         if ( /^\.(.*?)_END.$/ ) {
40                 if ("$1." ne $section) {
41                         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";
42                 }
43                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
44                 $section = '';
45                 next;
46         }
47
48         # Looks like we've got some actual data!  Let's store it.
49         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
50         if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
51
52                 # Build the name of this field (taking note of whether we're in a named section of data)
53                 $field = '';
54                 if ($section ne '') { 
55                         $field .= $section;
56                 }
57                 $field .= $1;
58
59                 # Store the field as a key of an array.  If it already exists, oh well, now it still exists.
60                 $unique_fields{$field} = 1;
61
62                 # Now we can actually store this line of data!
63                 $users[$serial]{$field} = $3;           
64
65                 # print STDERR "Data extracted: \$users[$serial]{'$field'} = '$3'\n";
66
67                 next;
68         }       
69
70         # This is the continuation of the previous line.
71         else {
72                 chomp($_);
73                 $users[$serial]{$field} .= ' ' . $_;
74                 # print STDERR "Appended data to previous field. \$users[$serial]{'$field'} is now '" . $users[$serial]{$field} . "'.\n";
75         }
76
77 }
78
79 print STDERR "Loaded " . scalar(@users) . " users.\n";
80
81
82 # Print a header line
83 print "SERIAL\t";
84 @sorted_fields = sort keys %unique_fields;
85 foreach $i (@sorted_fields) {
86         print "$i\t";
87 }
88 print "\n";
89
90
91 # Print the results
92 for (my $u = 0; $u < @users; $u++) {
93         print "$u\t";   
94         foreach $f (@sorted_fields) {
95                 if (defined $users[$u]{$f}) {
96                         print $users[$u]{$f};
97                 }
98         print "\t";
99         }
100         print "\n";
101 }
102
103 print STDERR "Wrote " . scalar(@users) . " users.\n";
104 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS