Created
March 21, 2018 00:19
-
-
Save jamestomasino/b5e8d103ebb3e595926ce5bb28fc8ee5 to your computer and use it in GitHub Desktop.
Generate contact files from vcard download
This file contains 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
# to run: awk -f vcard-to-contact.awk vcard-file.vcf | |
BEGIN { | |
FS = ";" | |
} | |
/BEGIN:VCARD/ { | |
# new contact, reset variables | |
c_n = "" | |
c_fn = "" | |
c_email = "" | |
c_tel = "" | |
c_bday = "" | |
c_adr = "" | |
} | |
/^EMAIL/ { | |
email = tolower( $2 ) | |
gsub(/\r/,"",email) | |
gsub(/.*:/,"",email) | |
# clean up email address and collect one on each line | |
if ( length( c_email ) == 0 ) { | |
c_email = email | |
} else { | |
c_email = c_email "\n" email | |
} | |
} | |
/^TEL/ { | |
tel = tolower( $2 ) | |
gsub(/\r/,"",tel) | |
gsub(/.*:/,"",tel) | |
# clean up email address and collect one on each line | |
if ( length( c_tel ) == 0 ) { | |
c_tel = tel | |
} else { | |
c_tel = c_tel "\n" tel | |
} | |
} | |
/^BDAY/ { | |
c_bday = substr( $1, 6 ) | |
gsub(/\r/,"",c_bday) | |
} | |
/^ADR/ { | |
street = $4 | |
city = $5 | |
state = $6 | |
zip = $7 | |
gsub(/\r/,"",zip) | |
# clean up email address and collect one on each line | |
if ( length( c_adr ) == 0 ) { | |
c_adr = street ", " city ", " state ", " zip | |
} else { | |
c_adr = c_adr "\n" street ", " city ", " state ", " zip | |
} | |
} | |
/^N:/ { | |
# use a mulitpart name if found | |
familyname = "" | |
familyname = substr( $1, 3 ) | |
firstname = "" | |
firstname = $2 | |
gsub( /\./, "", firstname ) | |
middlename = $3 | |
gsub( /\./, "", middlename ) | |
if ( length( middlename ) > 0 ) { | |
c_n = firstname " " middlename " " familyname | |
c_fn = tolower(firstname "." middlename "." familyname) | |
} else { | |
c_n = firstname " " familyname | |
c_fn = tolower(firstname "." familyname) | |
} | |
} | |
/END:VCARD/ { | |
# strip whitespace | |
sub(/^ */, "", c_fn) | |
sub(/ *$/, "", c_fn) | |
if ( length(c_fn) > 0 ) { | |
printf "Creating file: %s\n", c_n | |
printf "%s\n\n", c_n > c_fn | |
if ( length( c_email ) > 0 ) { | |
printf "%s\n\n", c_email >> c_fn | |
} | |
if ( length( c_tel ) > 0 ) { | |
printf "%s\n\n", c_tel >> c_fn | |
} | |
if ( length( c_adr ) > 0 ) { | |
printf "%s\n\n", c_adr >> c_fn | |
} | |
if ( length( c_bday ) > 0 ) { | |
printf "Birthday: %s\n\n", c_bday >> c_fn | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment