1#! /usr/bin/perl 2 3# $Id: ldifuniq.pl,v 1.1 2002/08/24 15:11:21 kartik_subbarao Exp $ 4 5=head1 NAME 6 7ldifuniq.pl - Culls unique entries from a reference file with respect to a 8comparison file. 9 10=head1 DESCRIPTION 11 12ldifuniq.pl takes as input two LDIF files, a reference file and a comparison 13file. Each entry in the reference file is compared to its counterpart in the 14comparison file. If it does not have a counterpart, or if the counterpart is 15not identical, the reference entry is printed to standard output. Otherwise no 16output is generated. This behavior is analogous to the -u option of the uniq 17command. 18 19=head1 SYNOPSIS 20 21ldifuniq.pl reffile.ldif cmpfile.ldif 22 23=head1 AUTHOR 24 25Kartik Subbarao E<lt>subbarao@computer.orgE<gt> 26 27=cut 28 29 30use MIME::Base64; 31 32use strict; 33 34 35my $reffile = $ARGV[0]; 36my $cmpfile = $ARGV[1]; 37 38die "usage: $0 reffile cmpfile\n" unless $reffile && $cmpfile; 39 40$/ = ""; 41 42 43sub getdn { 44 my $rec = shift; 45 my $dn; 46 47 1 while s/^(dn:.*)?\n /$1/im; # Handle line continuations 48 if (/^dn(::?) (.*)$/im) { 49 $dn = $2; 50 $dn = decode_base64($dn) if $1 eq '::'; 51 } 52 53 $dn; 54} 55 56open(CMPFH, $cmpfile) || die "$cmpfile: $!\n"; 57my (%cmpdnpos, $pos); $pos = 0; 58while (<CMPFH>) { 59 my $dn = getdn($_); 60 $cmpdnpos{$dn} = $pos; 61 $pos = tell; 62} 63 64open(REFFH, $reffile) || die "$reffile: $!\n"; 65while (<REFFH>) { 66 my $refrec = $_; $refrec .= "\n" if $refrec !~ /\n\n$/; 67 my $dn = getdn($refrec); 68 my $pos = $cmpdnpos{$dn}; 69 if ($pos eq undef) { 70 print $refrec; next; # Not in cmpfile, print the entry. 71 } 72 seek(CMPFH, $pos, 0); 73 my $cmprec = <CMPFH>; $cmprec .= "\n" if $cmprec !~ /\n\n$/; 74 print $refrec if $refrec ne $cmprec; 75} 76