#!/bin/rc
switch($#*){
case 0
echo 'usage: '$0' <vcf-file>'
exit usage
case 1
sed 's/
//g;
s/END:VCARD//g;
s/ /\\t/g;
' $1 | awk '
function telclass (telline) {
tel = ""
telrole = "home"
teltype = "fixed"
split(telline[1], telinfo, ";")
for (j = 2 ; j < 5 ; j++) { # Hack: why 5?
if (j in telinfo && telinfo[j] ~ "type=") {
split(telinfo[j], teltypes, "=")
if (teltypes[2] ~ /WORK|work|HOME|home/)
telrole = tolower(teltypes[2])
else if (teltypes[2] ~ /CELL|cell/)
teltype = "mobile"
else if (teltypes[2] ~ /FAX|fax/)
teltype = tolower(teltypes[2])
else if (teltypes[2] ~ /MAIN|main/) {
teltype = "fixed"
telrole = "work"
}
}
}
tel=telline[2]
printf("%s\t%s\t%s\t%s\n", NR, tel, teltype, telrole) > "D.tel"
}
function imclass (imline) {
im = ""
imrole = "other"
imtype = "unknown"
split(imline[1], iminfo, ";")
if (iminfo[1] ~ /X\-JABBER$/)
imtype = "jabber"
else if (iminfo[1] ~ /X\-AIM$/)
imtype = "aim"
for (j = 2 ; j < 5 ; j++) { # Hack: why 5?
if (j in iminfo && iminfo[j] ~ "type=") {
split(iminfo[j], imtypes, "=")
if (imtypes[2] ~ /WORK|work|HOME|home/)
imrole = tolower(imtypes[2])
}
}
im=imline[2]
printf("%s\t%s\t%s\t%s\n", NR, im, imtype, imrole) > "D.im"
}
function emaclass (emaline) {
ema = ""
emarole = "home"
split(emaline[1], emainfo, ";")
for (j = 2 ; j < 5 ; j++) { # Hack: why 5?
if (j in emainfo && emainfo[j] ~ "type=") {
split(emainfo[j], ematypes, "=")
if (ematypes[2] ~ /WORK|work|HOME|home/)
emarole = tolower(ematypes[2])
}
}
ema=oneline[2]
printf("%s\t%s\t%s\n", NR, ema, emarole) > "D.ema"
}
function addrclass (addrline) {
street = city = state = postcode = cc2 = ""
addrrole = "home"
split(addrline[1], addrinfo, ";")
for (j = 2 ; j < 5 ; j++) { # Hack: why 5?
if (j in addrinfo && addrinfo[j] ~ "type=") {
split(addrinfo[j], addrtypes, "=")
if (addrtypes[2] ~ /WORK|work|HOME|home/)
addrrole = tolower(addrtypes[2])
}
}
split(oneline[2], addr, ";")
street = addr[3]
city = addr[4]
state = addr[5]
postcode = addr[6]
cc2 = addr[7]
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
NR, addrrole, street, city, state, postcode, cc2) > "D.addr"
}
BEGIN {
RS = ""
FS = "\n"
c = 0
}
{
nick = first = middle = last = suff = org = dept = title = cats = ""
for (i = 1 ; i < NF ; i++) {
split($i, oneline, ":")
if (oneline[1] == "N") {
split(oneline[2], fullname, ";")
last = fullname[1]
first = fullname[2]
middle = fullname[3]
suff = fullname[4]
} else if (oneline[1] == "NICKNAME")
nick = oneline[2]
else if (oneline[1] == "ORG") {
split(oneline[2], orginfo, ";")
org = orginfo[1]
dept = orginfo[2]
} else if (oneline[1] == "TITLE")
title = oneline[2]
else if (oneline[1] == "CATEGORIES")
cats = oneline[2]
else if (oneline[1] ~ /^TEL;/)
telclass(oneline)
else if (oneline[1] ~ /^EMAIL;/)
emaclass(oneline)
else if (oneline[1] ~ /^(item[0-9]+\.)?X-(JABBER|AIM);/)
imclass(oneline)
else if (oneline[1] ~ /^(item[0-9]+\.)?ADR;/)
addrclass(oneline)
}
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
NR, nick, first, middle, last, suff, org, dept, title, cats) > "D.person"
}
END { print NR " vCards converted"}
'
case *
for(file in *)
vcf2pq $file
}
|