With a little side of applesauce...

Thursday, February 1, 2007

Converting Thunderbird address books to Mutt Aliases

In an attempt to get a handle on my email, I am trying out Mutt again. To help ease the transition, I wrote the following script which takes a CSV file that I have exported from Mozilla Thunderbird, and adds it to my mutt aliases file.


#!/bin/sh

usage() {
echo "Usage: $0 $1 [/path/to/csv/addressbook/file] [/path/to/mutt/aliases/file] "
exit 99
}
[ "$1" == "--help" ] && usage
[ $# -ne 2 ] && usage

CSVFILE=$1
MUTTALIASES=$2

# cat CSVFILE, then manipulate it line by line
cat $CSVFILE |
while read line
do
# The important fields for our mutt alias file are:
#
# 1 => firstname
# 2 => lastname
# 3 => Display Name
# 5 => email address
#
# This line takes the Display Name, (which is in the format: Firstname Lastname),
# and transforms it into "firstname_lastname"
namealias=$(echo ${line} | cut -d, -f3 | tr " " _ | tr "[:upper:]" "[:lower:]" | tr "\&" "and" | grep -v "'" | grep -v "\." | grep -v "-" | grep -v "^$")

# Now, grab the firstname, lastname, and email address
firstname=$(echo ${line} | cut -d, -f1)
lastname=$(echo ${line} | cut -d, -f2)
emailaddr=$(echo ${line} | cut -d, -f5)

# I didn't want any entries in the aliases file that were missing
# namealias, firstname, lastname, or emailaddr, so I just omit
# those entries. (Now that I look back, I might not need the
# grep -v "^$, when initializing namealias
if [ "$namealias" == "" ]; then
continue
elif [ "$firstname" == "" ]; then
continue
elif [ "$lastname" == "" ]; then
continue
elif [ "$emailaddr" == "" ]; then
continue
else
echo "alias $namealias $firstname $lastname $emailaddr" >> $MUTTALIASES
fi
done

No comments: