September 6, 2010

Android vCard Import

Android phone-book entries can be exported as vCard. However, the +6281XXXXXXXXXX number format can't be exported properly. It is broken because the “+” prefix is missing. The number has changed to 628-XXX-XXXXXXX, therefore it cannot be used to make a call.

I always get this problem every time I upgrade Android OS… So, I developed a simple AWK script to fix the problem. Here, the 6281 number prefix will be normalized to — example — 081.
The script filters lines started with “TEL” then extract the phone number and do normalization if necessary. Below is a sample of vCard entry.


vCard entry sample.
BEGIN:VCARD
VERSION:2.1
N:;Foo;;;
FN:Foo Bar
TEL;HOME;VOICE:021-234-56789
TEL;CELL:622-123-456789
END:VCARD

Some AWK stuffs

Here is some Awk functions which are used by the script.

Field separator is character(s) used as token to delimit string into variables. It can be defined dynamically every where inside the code with FS variable.

Matching expression “~” can be used like do match in PERL. It can be written like this ($0~/REGEXP/).

String manipulation functions are built-in. Some of these functions are being used.The
gsub(/REGEXP/, REPLACEMENT, STRING) which is used to do substitution in global scope, sub(/REGEXP/, REPLACEMENT, STRING) which is used for the same thing, but only in local scope.

The substr(STRING, IDX, LEN) which is used to slice phone number, and length(STRING) to get the length of a STRING.

Let assume the input filename is tes.vcf and the output filename is hore.vcf, and put them into following code.

awk '{
if($0~/TEL/){
FS=":"; 
TEL=$2; 
gsub(/-/,"",TEL); 
sub(/^62/,"0",TEL); 
CODE1=substr(TEL,1,3); 
CODE2=substr(TEL,4,3); 
CODE3=substr(TEL,7,length(TEL)-7); 
print $1":"CODE1"-"CODE2"-"CODE3
}else{
print $0;
}
};' tes.vcf > hore.vcf
Upload the hore.vcf file back to the phone, do the phone-book import, and done!

0 comments:

Post a Comment