#!/usr/bin/perl -w #---------------------------------# # PROGRAM: info_formatter.pl # # Eric Knibbe, May 2007 # #---------------------------------# # This program will, given a directory full of information requests as an argument, # generate a CSV file that can be easily made into a spreadsheet. # Exit if no arguments specified if (!@ARGV) { print " Usage: info_formatter.pl directory\n"; exit 1; } # Open the directory, specified by the first argument $in_directory = $ARGV[0]; opendir INDIR, $in_directory or die "Cannot open $in_directory: $!"; @infofiles = readdir INDIR; closedir INDIR; # Create an output file $csvFile_fullpath = "$in_directory/../inforequests.csv"; $dmt = ","; open OUTFILE, "> $csvFile_fullpath"; # print headings #print OUTFILE '"orgname"'.$dmt.'"address"'.$dmt.'"city"'.$dmt.'"state"'.$dmt.'"zip"'.$dmt.'"phone"'.$dmt.'"fax"'.$dmt.'"name"'.$dmt.'"email"'.$dmt.'"affiliation"'.$dmt.'"heard_from"'.$dmt.'"comments"'."\n"; print OUTFILE '"date"'.$dmt.'"email"'.$dmt.'"you_are_a"'.$dmt.'"name"'.$dmt.'"schoolname"'.$dmt.'"address"'.$dmt.'"city"'.$dmt.'"state"'.$dmt.'"zip"'.$dmt.'"phone"'.$dmt.'"fax"'.$dmt.'"heard_from"'.$dmt.'"comments"'."\n"; foreach $info (@infofiles) { # Skip any files with names starting with a period next if $info =~ /^\./; # Open an input file $infoFile_fullpath = "$in_directory/$info"; open INFILE, $infoFile_fullpath or die "Error opening input file"; @contents = ; foreach $line (@contents) { $line =~ s/=20/__/; $line =~ s/"/'/; } &munchline(12); # date $offset = 0; if (substr($contents[33], 0, 5) eq "-----") { $offset += 1; } &munchline(33+$offset); # email &munchline(34+$offset); # you_are_a &munchline(35+$offset); # name # $heard_from_line = 39+$offset; # heard_from # chomp($contents[$heard_from_line]); # $infoEntry .= '"'.$contents[$heard_from_line]; # until ($contents[$heard_from_line+1] eq "\n") { # $heard_from_line += 1; # chomp($contents[$heard_from_line]); # $infoEntry .= $contents[$heard_from_line]; # } # $infoEntry .= '"'.$dmt; &munchline(39+$offset); # schoolname $address_line = 40+$offset; &munchline($address_line); # address 1 until (substr($contents[$address_line+1], 0, 5) eq "Phone") { $address_line += 1; $contents[$address_line] =~ s{[\f\t\r ]+}{ }; # remove whitespace chomp($contents[$address_line]); # remove return character if ($contents[$address_line] =~ /, /) { @addressbits = split /, /, $contents[$address_line]; foreach $bit (@addressbits) { $infoEntry .= '"'.$bit.'"'.$dmt; } } else { chop($infoEntry); # remove " chop($infoEntry); # remove delimiter $infoEntry .= $contents[$address_line].'"'.$dmt; } } &munchline($address_line+1); # phone &munchline($address_line+2); # fax $heard_from_line = $address_line+7; # heard_from chomp($contents[$heard_from_line]); if (substr($contents[$heard_from_line], -1) eq "=") { chop($contents[$heard_from_line]); # lose the mysterious "=" sign } $infoEntry .= '"'.$contents[$heard_from_line]; # until ($contents[$why_honored_line+1] eq "\n") { until ($contents[$heard_from_line+1] eq "\n") { # stop when reaching a blank line $heard_from_line += 1; # next if ($contents[$why_honored_line] eq "\n"); chomp($contents[$heard_from_line]); if (substr($contents[$heard_from_line], -1) eq "=") { chop($contents[$heard_from_line]); # lose the mysterious "=" sign } $infoEntry .= $contents[$heard_from_line]; } $infoEntry .= '"'.$dmt; $comments_line = $heard_from_line+3; # comments chomp($contents[$comments_line]); if (substr($contents[$comments_line], -1) eq "=") { chop($contents[$comments_line]); # lose the mysterious "=" sign } $infoEntry .= '"'.$contents[$comments_line]; until (substr($contents[$comments_line+1], 0, 5) eq "-----") { # stop when reaching the end of the message content $comments_line += 1; chomp($contents[$comments_line]); if (substr($contents[$comments_line], -1) eq "=") { chop($contents[$comments_line]); # lose the mysterious "=" sign } $infoEntry .= $contents[$comments_line]; } $infoEntry .= '"'.$dmt; chop($infoEntry); $infoEntry .= "\n"; # print $infoEntry; $infoEntry =~ s/__/ /g; $infoEntry =~ s/ / /g; $infoEntry =~ s/\"\"/\" \"/g; print OUTFILE $infoEntry; $infoEntry = ""; print "-processed $info\n"; close INFILE; } close OUTFILE; print " Processing successful.\n"; sub munchline { $contents[$_[0]] =~ s{:[\f\t\r ]+}{: }; # shorten whitespace @pieces = split /: /, $contents[$_[0]]; # split line to isolate data chomp($pieces[1]); $infoEntry .= '"'.$pieces[1].'"'.$dmt; }