Starting all over - where did my contacts go?

Starting up a new company from scratch puts you into the position that you sooner or later have to get going and get started with simple things, that are no longer simple.

Why? Well, the structures you are used to, might not exist yet.

I am talking about basic features, like:

The other day, I wanted to call up an old mentor, Peter, for a quick discussion concerning customer aquisision. A two minute phone call, that took me 4 hours to complete…

I realized that I did not have all contacts I need in my mobile phone. People I got to know the last 5 years, were no longer in my new, fancy Nokia 2330 (which cost me 49 chf including a pre paid abo). The number I had listed for Peter was not correct, as I had typed it in wrong in the phone. I decided to do this properly, as this would for sure not be the only phone call to make today.

So… How were I to get all my 600+ phone numbers into this phone? They were all conveniently stored in a text file, semicolon separated, as I had no other means to save the old phone numbers from my old phone. Now I needed to get them all back in shape.

I work on a MacBook Pro, not having the infrastructure set up yet to centralize my contacts. I decided to use the Apple Phone Book and the iSync application. Given that I had already gotten the iSync to talk to my phone (after downloading the plugin for the phone here, thanks a lot!), I needed a way to convert my text file to vCards.

This is what I had to work with:

"Muster";"Musterson";"";"";"Industry Sales Consultant";;"muster.musterson@template.com";"";"";"+41 79 123 4567";"";"+41 44 123 4567";"";"";"";"";"other";"Mustergasse 123";"Bern";"Bern";"3018";"Switzerland";"";;;;;;;;;

I decided to look up the vCard format on one of the indisputable sources on the internet; Wikipedia. Here I got a sample of what to put into the vCard file:

BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;TYPE=WORK:100 Waters EdgenBaytown, LA 30314nUnited States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;TYPE=HOME:42 Plantation St.nBaytown, LA 30314nUnited States of America
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

Nice, at least now I know what it should look like.

People who know me, say that I am sometimes a bit too conservative when it comes to selecting technologies. I know that converting the text file to xml, using some readily made code for php, python, perl, or whatever, I could probably have converted the whole file quickly. But the file was in crude “;” separated form, so converting it first to xml, then creating the vCards, would have taken me even longer. I decided for some fairly new tools;

First, I felt that I need to create a template file for my vCards:

malu@ml-sst7-0001:/home/malu/temp/vcard $cat template.txt
BEGIN:VCARD
VERSION:3.0
N:XXX_LASTNAME_XXX;XXX_FIRSTNAME_XXX
FN:XXX_FIRSTNAME_XXX XXX_LASTNAME_XXX
ORG:XXX_ORG_XXX
TITLE:XXX_TITLE_XXX
TEL;TYPE=WORK,VOICE:XXX_WORKPHONE_XXX
TEL;TYPE=HOME,VOICE:XXX_HOMEPHONE_XXX
TEL;TYPE=CELL:XXX_MOBILEPHONE_XXX
ADR;TYPE=HOME:;;XXX_STREET_ADDRESS_XXX;XXX_CITY_XXX;XXX_STATE_XXX;XXX_ZIP_XXX;XXX_COUNTRY_XXX
LABEL;TYPE=HOME:XXX_STREET_ADDRESS_XXXnXXX_ZIP_XXXnXXX_CITY_XXXnXXX_COUNTRY_XXX
EMAIL;TYPE=PREF,INTERNET:XXX_EMAIL_XXX
NOTE:XXX_NOTE_XXX
REV:XXX_CURDATE_XXXTXXX_CURTIME_XXXZ
END:VCARD

My favorite naming convention to create unique strings in a template file came in handy; XXX_parameter_XXX

The attributes I wanted to use were:

malu@ml-sst7-0001:/home/malu/temp/vcard $cat template.txt | perl -pe 's/.*?(XXX_.*?_XXX).*?/1n/g' | grep XXX | grep -v "^$" | sort -u
XXX_CITY_XXX
XXX_COUNTRY_XXX
XXX_CURDATE_XXX
XXX_CURTIME_XXX
XXX_EMAIL_XXX
XXX_FIRSTNAME_XXX
XXX_HOMEPHONE_XXX
XXX_LASTNAME_XXX
XXX_MOBILEPHONE_XXX
XXX_NOTE_XXX
XXX_ORG_XXX
XXX_STATE_XXX
XXX_STREET_ADDRESS_XXX
XXX_TITLE_XXX
XXX_WORKPHONE_XXX
XXX_ZIP_XXX

Now, I only needed to convert the text file (grab the columns) and translate them into a vCard using my template file.

I will start by showing you a crude hack to grab a column from a line, using a neat little function in Korn shell, get_column(). The next step would be to put this all into the template file, which was done in another (rather ugly) function using global variables, gen_vcard().

#--- gen_vcard()
gen_vcard() {
cat $template_file | grep -v "^#" | sed 
-e 's/XXX_FIRSTNAME_XXX/'"$cur_firstname"'/g' 
-e 's/XXX_LASTNAME_XXX/'"$cur_lastname"'/g' 

}
#--- get_col() separator column long_string_of_separated_columns
get_col() {
separator="$1"
column=$2
shift
shift
cur_line=$*
cur_value=$(
echo "$cur_line" | cut -d ";" -f $column | sed -e 's/"//g' -e 's!/!\\/!g'
)
echo $cur_value
}

for line in `grep "$pattern" $input_file`
do
print ======== new line ==========
cur_firstname=`get_col ";" 1 "$line"`
cur_lastname=`get_col ";" 2 $line`
# ... more attributes set...
print First name: $cur_firstname Last name: $cur_lastname
gen_vcard > ${cur_firstname}.${cur_lastname}.vcf
done

Given all this together, I end up with a vCard, which I simply can open in my file browser to import into my Address Book, then sync it with my phone. 600+ contacts were created in ca a minute or so.

malu@ml-sst7-0001:/home/malu/temp/vcard $./gen_vcard "Muster"
======== new line ==========
---- 1: vcard for Muster Musterson
malu@ml-sst7-0001:/home/malu/temp/vcard $cat vcards/Muster.Musterson.vcf
BEGIN:VCARD
VERSION:3.0
N:Musterson;Muster
FN:Muster Musterson
ORG:
TITLE:
TEL;TYPE=WORK,VOICE:+41 44 123 4567
TEL;TYPE=HOME,VOICE:
TEL;TYPE=CELL:+41 79 123 4567
ADR;TYPE=HOME:;;Mustergasse 123;Bern;;3018;Switzerland
LABEL;TYPE=HOME:Mustergasse 123n3018nBernnSwitzerland
EMAIL;TYPE=PREF,INTERNET:muster.musterson@template.com
NOTE:
REV:20100614T114029Z
END:VCARD

Easy, made simple…