Skip to content

Commit

Permalink
Added option to sort attributes within each entry.
Browse files Browse the repository at this point in the history
  • Loading branch information
kartiksubbarao committed Jun 24, 2003
1 parent fdf63eb commit 49c7a41
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions contrib/ldifsort.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/perl

# $Id: ldifsort.pl,v 1.1 2001/07/03 19:30:02 gbarr Exp $
# $Id: ldifsort.pl,v 1.2 2003/06/24 21:58:34 kartik_subbarao Exp $

=head1 NAME
Expand All @@ -13,7 +13,7 @@ =head1 DESCRIPTION
=head1 SYNOPSIS
ldifsort.pl B<-k keyattr> [B<-nd>] file.ldif
ldifsort.pl B<-k keyattr> [B<-and>] file.ldif
=over 4
Expand All @@ -23,6 +23,12 @@ =head1 SYNOPSIS
specified, sorting is done by the full DN string, which can be composed of
different attributes for different entries.
=item B<-a>
Specifies that attributes within a given entry should also be sorted. This
has the side effect of removing all comments and line continuations in the
LDIF file.
=item B<-n>
Specifies numeric comparisons on the key attribute. Otherwise string
Expand Down Expand Up @@ -51,9 +57,10 @@ =head1 AUTHOR
use strict;

my %args;
getopts("k:nd", \%args);
getopts("k:and", \%args);

my $keyattr = $args{k};
my $sortattrs = $args{a};
my $ldiffile = $ARGV[0];

die "usage: $0 -k keyattr [-n] [-d] ldiffile\n"
Expand Down Expand Up @@ -96,7 +103,14 @@ sub cmpdn {
foreach my $valuepos (@sorted) {
seek(LDIFH, $valuepos->[1], 0);
my $entry = <LDIFH>;
print $entry;
print "\n" if $entry !~ /\n\n$/;
if ($sortattrs) {
$entry =~ s/\n //mg; # collapse line continuations
my @lines = grep(!/^#/, split(/\n/, $entry));
my $dn = shift(@lines);
print "$dn\n", join("\n", sort @lines), "\n\n";
}
else {
print $entry;
print "\n" if $entry !~ /\n\n$/;
}
}

0 comments on commit 49c7a41

Please sign in to comment.