Convert a CSV file to vCard format
Recently I was faced with the problem of having to convert a CSV file into vCard entries. With 10 or 20 entries you might still be able to do this manually, but I had over 200 entries to transfer. From this volume at the latest, it is worth thinking about a suitable script that performs this task reliably. For all those who also have to perform this task (and possibly have one more entry to transfer), I have made the script available here:
#!/bin/bash
# Script:
# Version: 1.0
# Autor: Marco Morath
# Lizenz: Gnu GPLv3 <https://www.gnu.org/licenses/gpl-3.0.html>
# Anwenderhinweis:
# Das Skript erzeugt aus jeder Zeile einer CSV-Datei einen vcard-Eintrag in einer vcard-Datei.
# Somit kann aus einer CSV-Datei mit Kontakten eine importierbare vcard-Datei erzeugt werden.
# Nach dem Dateinamen ist (ggf. mit relativem Pfad) die CSV-Datei anzugeben.
# Als Ausgabe wird die Datei contacts.vcf erzeugt.
# Überprüfen, ob die Eingabedatei angegeben wurde
if [ "$#" -ne 1 ]; then
echo "Verwendung: $0 <input.csv>"
exit 1
fi
# Eingabedatei
INPUT_FILE=$1
# Ausgabedatei
OUTPUT_FILE="contacts.vcf"
# Leere die Ausgabedatei, falls sie existiert
> $OUTPUT_FILE
# Überspringe die Kopfzeile und lese die CSV-Zeilen
# +2 = Kopfzeile überspringen
# Es müssen alle CSV-Felder in der Reihenfolge der CSV-Datei eingegeben werden (ob diese genutzt werden oder nicht)
# Weitere Informationen zu den Feldbezeichnungen: https://de.wikipedia.org/wiki/VCard#Spezifikation
tail -n +2 $INPUT_FILE | while IFS=';' read -r Typ Vorname Nachname Firma Position Hinweis Schlagworte Telefon_privat Telefon_arbeit Telefon_mobil Fax Telefon_sonstige EMail_privat EMail_arbeit EMail_sonstige Strasse Ort Bundesland PLZ Land
do
cat >> $OUTPUT_FILE <<EOL
BEGIN:VCARD
VERSION:3.0
FN;CHARSET=UTF-8:$Vorname $Nachname
N;CHARSET=UTF-8:$Nachname;$Vorname;;;
KIND;CHARSET=UTF-8:$Typ
TEL;type=HOME;VOICE:$Telefon_privat
TEL;type=WORK;VOICE:$Telefon_arbeit
TEL;type=HOME;CELL:$Telefon_mobil
TEL;type=OTHER;VOICE:$Telefon_sonstige
TEL;type=HOME;FAX:$Fax
TITLE;CHARSET=UTF-8:$Position
EMAIL;type=HOME:$EMail_privat
EMAIL;type=WORK:$EMail_arbeit
EMAIL;type=OTHER:$EMail_sonstige
CATEGORIES;CHARSET=UTF-8:$Schlagworte
NOTE;CHARSET=UTF-8:$Hinweis
ORG;CHARSET=UTF-8:$Firma
ADR;type=WORK;CHARSET=UTF-8:;;$Strasse;$Ort;$Bundesland;$PLZ;$Land
END:VCARD
EOL
done
echo "VCF-Datei erfolgreich erstellt: $OUTPUT_FILE"
Although the vCard format is specified, not all software adheres to it or an old version is being used. It is therefore quite possible that the import with the vcf file generated by this script will not work. In this case, it is recommended to create a sample entry in the software, export it and use the exported entry to adapt the script so that the import format meets the requirements of the software.