Skip to content

Instantly share code, notes, and snippets.

@tankred
Created October 10, 2019 13:22
Show Gist options
  • Save tankred/02648b08eeb36657a8797ea204e32b83 to your computer and use it in GitHub Desktop.
Save tankred/02648b08eeb36657a8797ea204e32b83 to your computer and use it in GitHub Desktop.
export CSV to Vcard
#!/usr/bin/perl
use strict;
use Text::vCard;
use Text::vCard::Addressbook;
my @contacts;
open(F,"<$ARGV[0]") or die("Can't open $ARGV[0]: $!\n");
foreach my $row (<F>) {
my $index = 0;
my %c = (); my @p = split /\|/, $row;
# Skip rows that don't have enough columns
next if (scalar @p != 7);
foreach (qw/first last company home mobile email1 email2/) {
$c{$_} = $p[$index]; $index ++;
}
push @contacts, \%c;
}
close(F);
# Create a new address book
my $address_book = Text::vCard::Addressbook->new();
foreach my $r (@contacts) {
my %c = %{$r};
my $vcard = $address_book->add_vcard();
$vcard->fullname( "$c{first} $c{last}" );
$vcard->email( $c{email1} );
my $name = $vcard->add_node({'node_type'=>'N'});
$name->family( $c{last} ); $name->given( $c{first} );
my $mobil = $vcard->add_node({'node_type'=>'TEL'});
$mobil->add_types('cell'); $mobil->value($c{mobile});
$mobil = $vcard->add_node({'node_type'=>'TEL'});
$mobil->add_types('home'); $mobil->value($c{home});
}
print $address_book->export();
@tankred
Copy link
Author

tankred commented Oct 10, 2019

I do not remember where I found this script somewhere in 2012.
31/05/2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment