Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Format LDIF file for Address Book import' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Format LDIF file for Address Book import
Authored by: triplef on Apr 18, '03 11:46:36AM

Would be nice if you could post the perl template as well!

Thanks,
TripleF



[ Reply to This | # ]
Format LDIF file for Address Book import
Authored by: davidfmartin on Apr 18, '03 05:42:52PM
I can't post the actual code because of legal reasons but here is a simple sample.

Here is the actual data file. If you cut and paste this there are 3 lines of comments followed by 2 lines of data. The file should be saved as "addresses.txt".

# file format is as follows
# emailx|firstx|lastx|office_phone|fax|pager|cell_phone|office_location|center|d
escription
#
davidfmartin@yahoo.com|David|Martin|303-555-1212|303-555-1313|303-555-1414|303-5
55-1515|17/275-09|3100|Mailstop - 12275
lainie_meilinger@noreply.com|Lainie|Meilinger|303-555-9000|303-555-9001|303-555-
9002|303-555-9003|17/275-10|8100|Mailstop - 12275, My lovely wife.

This is the template file and should be saved as "addresses.tmpl".

dn: cn={$firstx} {$lastx},mail={$emailx}
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
givenName: {$firstx}
sn: {$lastx}
cn: {$firstx} {$lastx}
mail: {$emailx}
TelephoneNumber: {$office_phone}
FacsimileTelephoneNumber: {$fax}
Pager: {$pager}
Mobile: {$cell_phone}
homePostalAddress: {$office_location}
ou: {$center}
O: Perl Programmers Inc.
description: {$description}

This is the perl program and should be saved as AddressBook.pl. You will have to change the perl path at the top to match your perl path

!/sw/bin/perl
# 
# Author: David Martin (davidfmartin@yahoo.com)
# Created: 04/18/2003
# Purpose: Example Program for creating an LDIF file from a simple pipe 
# delimeted flat file for import into OSX Address Book
# 
# Modifications
# -------------

# you must install this from CPAN to parse the template.
use Text::Template;  

# data files 
$IN	= 'addresses.txt';
$OUT 	= 'addresses.ldif';
$TMPL	= 'addresses.tmpl';

# load template parsing module
$tmplConfig = new Text::Template(
        TYPE   => FILE,
        SOURCE => "$TMPL"
);


open( $OUT, ">$OUT") or die( "$0: Unable to open output file: $OUT\n" );
open( $IN, "$IN" ) or die( "$0: Unable to open input file: $IN\n" );

while(  ) {
    	next if /^#/; 	# skip if line stars with comment
	chomp; 		# strip trailing newline
	
	( $emailx, $firstx, $lastx, $office_phone, $fax, 
	  $pager, $cell_phone, $office_location, $center, 
	  $description ) = split(/\|/, $_);				# split on | 
  
	print $OUT $tmplConfig->fill_in();				# write filled in template to output file
}    

close( $IN );
close( $OUT );



[ Reply to This | # ]