Skip to content

Instantly share code, notes, and snippets.

@jaromil
Last active March 26, 2021 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaromil/bb6aeecbfcfd036533480a725afa674b to your computer and use it in GitHub Desktop.
Save jaromil/bb6aeecbfcfd036533480a725afa674b to your computer and use it in GitHub Desktop.
Shell script to convert a VCard file to a CSV compatible to import in Outlook
#!/bin/zsh
# Shell script to convert a VCard to a CSV to import in Outlook
#
# Requirements: Zsh, GNU AWK and unix2dos
#
# Usage:
# cat Contacts.vcf | ./vcard2outlook.sh | unix2dos > Contacts.csv
#
# * Copyright (C) 2021 Dyne.org foundation
# * designed, written and maintained by Denis Roio <jaromil@dyne.org>
# *
# * This program is free software: you can redistribute it and/or modify
# * it under the terms of the GNU Affero General Public License v3.0
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU Affero General Public License for more details.
# *
# * Along with this program you should have received a copy of the
# * GNU Affero General Public License v3.0
# * If not, see http://www.gnu.org/licenses/agpl.txt
tmp=`mktemp`
cat | awk '
BEGIN { c=0; nick=""; name=""; email=""; }
/^NICKNAME:/ { gsub(/\015/,""); nick=$0 }
/^FN:/ { gsub(/\015/,""); name=$0 }
/^EMAIL/ { gsub(/\015/,""); email=$0 }
/^END:VCARD/ {
if(email != "") {
c = c + 1
split(nick,k,":")
print k[2]
split(name,n,":")
print n[2]
split(email,e,":")
print e[2]
print "# " c
}
nick=""
email=""
name=""
}
' > $tmp
cat << EOF
First Name,Last Name,E-mail Address,E-mail Display Name
EOF
for i in "${(f)$(cat $tmp)}"; do
if [[ "$nick" = "" ]]; then nick="$i"; continue; fi
if [[ "$name" = "" ]]; then name="$i"; continue; fi
if [[ "$email" = "" ]]; then email="$i"; continue; fi
if [[ "${i[1]}" == "#" ]]; then
if [[ "$nick" == "$email" ]]; then nick=""; fi
if [[ "$name" == "$email" ]]; then name=""; fi
print -- "${nick},${name},${email},$nick $name"
nick=""; name=""; email=""
continue
fi
done
rm -f $tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment