[Users] the adressbook: how can i copy/export simple name + eMail adress?

Owen rcook at pcug.org.au
Fri Nov 18 21:22:08 CET 2011


On Fri, 18 Nov 2011 11:46:34 +0100
Jens <jens at 9dcf12.de> wrote:

> Hello,
> 
> yesterday i would simple copy a list of adresses from the
> CM-Adressbook to a text file or via copy & paste to a text file or
> drag & drop to a text file, like this format:
> 
> name1 name1 at mail.mm
> name2 name2 at mail.mm
> name3 name3 at mail.mm
> ...
> 
> or this 
> 
> name1 name1 at mail.mm, name2 name2 at mail.mm, name3 name3 at mail.mm, ...
> 
> But not easy way found to do this. The only way i found was the
> export via LDIF. But in this exported file are many other
> informations, like this:
> 
> dn: cn=name1,zzxzx
> objectClass: inetOrgPerson
> cn: name1
> sn: Some SN
> displayName: Name1
> mail: name1 at mail.mm
> 
> dn: cn=name2,zzxzx
> objectClass: inetOrgPerson
> cn: name2
> sn: Some SN
> displayName: Name2
> mail: name2 at mail.mm
> 
> ...
> 
> Is a easy way available to do this i wish?


Here are a couple of perl scripts I wrote to do this. Just watch for
any line wrap

From with the addrbook directory
==============================================
#!/usr/bin/perl

use XML::Simple;

#use Data::Dumper; #For debugging
use strict;

my $xml = new XML::Simple( KeyAttr => [], ForceArray => 1 );

my $data = $xml->XMLin("addrbook-000004.xml"); 

#print Dumper($data); #For debugging

foreach my $d ( @{ $data->{person} } ) {
print $d->{cn} . "\t";
print $d->{'address-list'}[0]->{'address'}[0]->{'email'} . "\n";
}
=============================================

and from the exported ldif of the address book


=============================================
#!/usr/bin/perl

use strict;
my $file = shift;      #Just pass the first part of the file name.
my $ldif_addresses = "/home/owen/Lanceslist.ldif"; #ldif extension
my $ldif_sorted = "/home/owen/$file.sorted";
open(my $FH, "$ldif_addresses" ) or die "cant open $ldif_addresses $!\n";
open(my $OF, ">$ldif_sorted" ) or die "cant open $ldif_addresses $!\n"; 
while (<$FH>) {
chomp;
my $line = $_;
if ( $line =~ /cn: / ) { print $OF "$'\t"; }
if ( $line =~ /mail: / ) { print $OF "$'\n"; }

}
close $OF;
close $FH;
open( $FH, "$ldif_sorted" ) or die "cant open $ldif_sorted $!\n";
my @list = <$FH>;
print " ";
@list = sort { $a cmp $b } @list;
print "@list";


==================================================


Owen






More information about the Users mailing list