#!/usr/bin/perl # # Copyright (c) 2012 Corey Edwards. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. use strict; use warnings; use Text::CSV; use Data::Dumper; my $csv = Text::CSV->new({binary => 1}); my $filename = shift || 'google.csv'; open(my $file, '<', $filename) || die "error opening file: $!"; my $row = $csv->getline($file); my $count = 0; while (!$csv->eof){ $count++; # there are 3 columns of phone numbers &do_tn($row->[34], $row->[33], $row->[0], $row->[49]); &do_tn($row->[36], $row->[35], $row->[0], $row->[49]); &do_tn($row->[38], $row->[37], $row->[0], $row->[49]); $row = $csv->getline($file); } close($file); sub do_tn { my $tn = shift; my $type = shift; my $name = shift; my $org = shift; # check for empty names if (!$name){ $name = $org; } if (!$name){ return; } # ignore the header if ($tn =~ m/Phone/){ return; } # fix apostrophes in names $name =~ s/'/'"'"'/g; # more than one phone number of a give type will be separated # by ::: my @tn = (); if ($tn =~ m/:::/){ @tn = split(/:::/, $tn); } else{ @tn = ($tn); } # print them out foreach $tn (@tn){ $tn = &format_tn($tn); if ($tn && $tn =~ m/1\d{10}/){ print "asterisk -rx 'database put cid $tn \"$name ($type)\"'\n"; } } } sub format_tn { my $tn = shift || ''; # just the numbers, maam $tn =~ s/[\-\(\) \+]//g; if ($tn =~ m/^\d{10}$/){ $tn = "1$tn"; } return $tn; }