Skip to content

Instantly share code, notes, and snippets.

@chuckwagoncomputing
Created February 7, 2018 21:56
Show Gist options
  • Save chuckwagoncomputing/0af9937971472d44e6932b8cab7db382 to your computer and use it in GitHub Desktop.
Save chuckwagoncomputing/0af9937971472d44e6932b8cab7db382 to your computer and use it in GitHub Desktop.
Script to convert postgres table to vcard
#!/usr/bin/env bash
I=0
while read t; do
I=$((I+1))
ID=$(echo $t | cut -d "|" -f 1 | xargs)
FIRST=$(echo $t | cut -d "|" -f 2 | xargs)
LAST=$(echo $t | cut -d "|" -f 3 | xargs)
ADDRESS=$(echo $t | cut -d "|" -f 4 | xargs)
CITY=$(echo $t | cut -d "|" -f 5 | xargs)
STATE=$(echo $t | cut -d "|" -f 6 | xargs)
ZIP=$(echo $t | cut -d "|" -f 7 | xargs)
MOBILE=$(echo $t | cut -d "|" -f 8 | xargs)
HOME=$(echo $t | cut -d "|" -f 9 | xargs)
WORK=$(echo $t | cut -d "|" -f 10 | xargs)
UUID=$(cat /proc/sys/kernel/random/uuid | tr [a-z] [A-Z])
VCD="""BEGIN:VCARD
VERSION:3.0
UID:$UUID
PRODID:-//Sabre//Sabre VObject 3.5.3//EN
REV:2018-02-07T16:44:55Z
N:$LAST;$FIRST;;;
FN:$FIRST $LAST
NOTE:$ID
X-ABSHOWAS:INDIVIDUAL"""
if [ $(echo -n $MOBILE | wc -c) -gt 0 ]; then
VCD="$VCD""""
TEL;TYPE=mobile:$MOBILE"""
fi
if [ $(echo -n $HOME | wc -c) -gt 0 ]; then
VCD="$VCD""""
TEL;TYPE=home:$HOME"""
fi
if [ $(echo -n $WORK | wc -c) -gt 0 ]; then
VCD="$VCD""""
TEL;TYPE=work:$WORK"""
fi
VCD="$VCD""""
ADR;TYPE=home:;;$ADDRESS;$CITY;$STATE;$ZIP;
END:VCARD"""
echo "$VCD" > $UUID.vcf
done <customers.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment