Original post is here: eklausmeier.goip.de
Sometimes vCard files need to be split into smaller files, or the file needs to be protected against merging in another application.
1. Splitting. Below Perl script splits the input file into as many files as required. Output files are named adr1.vcf, adr2.vcf, etc. You can pass a command line argument "-n
" to specify the number of card records per file. Splitting a vCard file is provided in palmadrsplit
on GitHub:
1use Getopt::Std;
2
3my %opts;
4getopts('n:',\%opts);
5my ($i,$k,$n) = (1,0,950);
6$n = ( defined($opts{'n'}) ? $opts{'n'} : 950 );
7
8open(F,">adr.$i.vcf") || die("Cannot open adr.$i.vcf for writing");
9while (<>) {
10 if (/BEGIN:VCARD/) {
11 if (++$k % $n == 0) { # next address record
12 close(F) || die("Cannot close adr.$i.vcf");
13 ++$i; # next file number
14 open(F,">adr.$i.vcf") || die("Cannot open adr.$i.vcf for writing");
15 }
16 }
17 print F $_;
18}
19close(F) || die("Cannot close adr.$i.vcf");
This is required for Google Contacts, as Google does not allow to import more than 1,000 records per day, see Quotas for Google Services.
2. Anti-Merge. Inhibiting annoying merging is given in file palmantimerge
on GitHub. Overall logic is as follows: Read entire vCard file and each card, delimited by BEGIN:VCARD
and END:VCARD
, is put on a hashmap. Each hashmap entry is a list of vCards. Hash key is the N:
entry, i.e., the concatentation of lastname and firstname. Once everything is hashed, then walk through hash. Those hash entries, where the list contains just one entry, can be output as is. Where the list contains more than one entry, then these entries would otherwise be merged, and then the N:
part is modified by using the ORG:
field.
1use strict;
2my @singleCard = (); # all info between BEGIN:VCARD and END:VCARD
3my ($name) = ""; # N: part, i.e., lastname semicolon firstname
4my ($clashes,$line,$org) = (0,"","");
5my %allCards = {}; # each entry is list of single cards belonging to same first and lastname, so hash of array of array
6
7while (<>) {
8 if (/BEGIN:VCARD/) {
9 ($name,@singleCard) = ("", ());
10 push @singleCard, $_;
11 } elsif (/END:VCARD/) {
12 push @singleCard, $_;
13 push @{ $allCards{$name} }, [ @singleCard ];
14 } else {
15 push @singleCard, $_;
16 $name = $_ if (/^N:/);
17 }
18}
19
20for $name (keys %allCards) {
21 $clashes = $#{$allCards{$name}};
22 for my $sglCrd (@{$allCards{$name}}) {
23 if ($clashes == 0) {
24 for $line (@{$sglCrd}) { print $line; }
25 } else {
26 $org = "";
27 for $line (@{$sglCrd}) {
28 $org = $1 if ($line =~ /^ORG:([ \-\+\w]+)/);
29 }
30 for $line (@{$sglCrd}) {
31 $line =~ s/;/ \/${org}\/;/ if ($line =~ /^N:/);
32 print $line;
33 }
34 }
35 }
36}
Every lastname is appended with "/organization/
" if the combination of firstname and lastname is not unique. For example, two records with Peter Miller in ABC-Corp and XYZ-Corp, will be written as N:Miller /ABC-Corp/;Peter
and N:Miller /XYZ-Corp/;Peter
.
This way Simple Mobile Tools Contacts from Simple Mobile Tools will not merge records together which it shouldn't. Issue #446 for this is on GitHub.