HOW TO: Copy Outlook Contacts to Abook

By Sterling “Chip” Camden
Contributing Writer, [GAS]

In the mass migration of end users from Windows to Unix-based workstations, one question that’s asked over and over again is “What will I ever do without Outlook?”

Wait, wait, CUT!  Let’s try that again, from the top.

In the trickle of tech-savvy users who’ve ditched Windows for a better computer, one question that’s asked over and over again is “How did I ever use that pig, Outlook?” — often followed immediately by “How do I get all my Outlook contacts moved over to something else?”

I recently converted to FreeBSD for my main squeeze, and decided on mutt for my new email client, based on recommendations from Chad, Mackenzie, and almost every other console-oriented *n*x geek out there. Mutt, like most good Unix programs, does one thing well: email.  Its support for contact management is minimized to the concept of aliases:  short names associated with full addresses, nothing more.  However, there are a few choices of contact managers that work well with mutt, allowing you to look up contacts while still within mutt.  One that’s specifically designed for use with mutt is abook.  It’s lightweight, uses a simple text file as its database, and provides support for several import/export formats, including CSV (comma-separated values).

Outlook provides an export to CSV.  While in Contacts, select “File/Import and Export…” (Office 2007).

  • Select “Export to a file”, and click “Next”.
  • Select “Comma Separated Values.” There are two possible options here, DOS or Windows. It doesn’t seem to matter which one.  Click “Next”.
  • Select your “Contacts” folder, and click “Next”.
  • Save the file.  I saved mine directly to a Samba-shared folder on my FreeBSD system.  Otherwise, you can copy it over later.
  • Just click “Finish.”  Even though the order of fields as output by Outlook does not match what abook expects, we’ll reorder them with a Ruby script instead of fiddling with the “Map Custom Fields” GUI.  That’s because we also want to combine some fields that Outlook separates.

Now, on the Unix side, you’ll need this script:


#!/usr/bin/env ruby
class String
  def q
    '"' + self + '"'
  end
  def u
    self.sub(/^"(.*)"$/, '\1')
  end
  def c
    self + ','
  end
end
class NilClass
  def u
    ""
  end
  def c
    ','
  end
end

full = ''
$<.each do |line|
  full += line.gsub!(/[\r\n]*/,'')
  if /(,|[^,]")$/ =~ line
    f = full.split /,(?=[,"]|$)/, -1
    full = ''
    print  f[3].c +     # last name
         (f[2].length > 2 ? (f[1].u + ' ' + f[2].u).q : f[1]).c + # first
         f[5].c +       # notes (company)
         (f[3].u + ', ' + f[1].u + ' ' + f[2].u).q.c +          # nickname
         f[31].c +      # workphone
         f[37].c +      # phone
         f[30].c +      # fax
         f[40].c +      # mobile
         f[57].c +      # email
         (f[8].u + ' ' + f[9].u + ' ' + f[10].u).q.c +          # address
         f[11].c +      # city
         f[12].c +      # state
         f[13].c +      # zip
         f[14].c +      # country
         f[87].c + f[88].c + f[89].c + f[90].c + f[91].c +      # custom
         "\n"
  end
end

Save that as ol2ab.rb, and make sure it’s executable.  Then create an intermediate csv file with this shell command:

ol2ab.rb < contacts.CSV > tmpfile.csv

That remaps the columns in the order that abook expects for its “Palm CSV” format.  I chose “Palm CSV” instead of just “CSV” because it imports more fields.  I also mapped “Company” to “notes”, because abook doesn’t have a Company field.  For “nick” (Nickname), I combined the name fields (Last, First M).  And I mapped the four user-defined fields plus the “Web Page” field from Outlook to the five “custom” fields in abook. You may also notice from the script that I account for an odd line-wrapping, the cause of which I do not know.  Some lines of the Outlook-generated CSV file were wrapped on a space.  So, I collect lines until they end appropriately – with either a comma or a quote, but not with the comma-quote combination.  Then I split them into fields on a comma, but only if it is followed by another comma, a quote, or the end of the line.  The regex-savvy amongst you readers will realize that this can be broken by a text field that ends with a comma.  Oh well.

Next, run the new csv file into an abook address file.  First, install abook.  On FreeBSD, it’s in ports in /usr/ports/mail/abook.  Make sure that the directory ~/.abook exists and does not contain the file ‘addressbook’.  Then execute this shell command:

abook --convert --infile tmpfile.csv --informat palmcsv --outfile ~/.abook/addressbook --outformat abook

Voila.  Not all the fields from Outlook have been brought over, but a lot of the important ones were.


Geeks are Sexy needs YOUR help. Learn more about how YOU can support us here.