Last active
May 2, 2025 03:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
# * | |
# * 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
I like your solution.
Perhaps simplify a bit:
Instead of
gsub(/\015/,""); ...
just replace "cat" with "tr -d '\r'" at the beginning
Instead of
split(foo,bar,":")
print bar[2]
just start with
awk -F":"
and you then use $2 instead of bar[2]
Hope this helps.
Cheers,
-- p