#!/usr/bin/perl # # author: ken macinnis # # purpose: converts a csv file from outlook into a format # suitable for import to nokia's datasuite format # # date: 30 may 2002 # # known problems: may not handle " or , in input records well use strict; my ($INFILE_NAME, $OUTFILE_NAME, $pname); ( $pname = $0 ) =~ s!.*/!!; $INFILE_NAME = @ARGV[0]; $OUTFILE_NAME = @ARGV[1]; # edumacate the user unless ($ARGV[0]) { usage(); } # will fix this later if ($ARGV[0] eq $ARGV[1]) { die "$pname: output file is input file\n"; } # give a default filename if needed unless ($ARGV[1]) { $INFILE_NAME =~ m/([^.]+)\.{1}(.*)/; $OUTFILE_NAME = $1 . "-out." . $2; } open(INFILE, '<', $INFILE_NAME) or die "Can't open input file $INFILE_NAME: $!\n"; open(OUTFILE, '>', $OUTFILE_NAME) or die "Can't open output file $OUTFILE_NAME: $!\n"; my ($fname, $first, $middle, $last, $lcv, $count, $outcount, @phones); ; # eat outlook's header while () { $count++; # strip the newline, remove commas around fields, translate phone number(s) to all digits chomp; s/"//g; s/\(?([[:digit:]]{3})\)?[^[:digit:]]*([[:digit:]]{3,10})[^[:digit]]*([[:digit:]]{4})/$1$2$3/g; ($first,$middle,$last,@phones[0..3]) = split(","); if ($middle) { $fname = "$first $middle $last"; } elsif ($last) { $fname = "$first $last"; } else { $fname = "$first"; } # I've found the max length of a name in the phone to be 16 $fname = substr($fname, 0, 16); print OUTFILE "$fname,@phones[0]\n"; $outcount++; # create more records if there is more than one phone number # additional records have "2, 3, 4 .." appended to the original name # shorten the string to make it of length 16 - (space) - length of prepended string foreach $lcv (1 .. $#phones) { if (@phones[$lcv]) { $fname = substr($fname, 0, ( 16 - 1 - length($lcv + 1) )); print OUTFILE "$fname " . ( $lcv + 1 ) . ",@phones[$lcv]\n"; $outcount++; } } } # *quick* hack. it may not work for you. take cover. system("sort $OUTFILE_NAME -o $OUTFILE_NAME"); print "Processed $count records", ($outcount > $count) ? ", created $outcount records" : "", "\n"; unless ($ARGV[1]) { print "Output file is $OUTFILE_NAME\n"; } # print some exciting informational messages sub usage { die <