ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The format doesn't to be as above.. but basically print the dn line and print how many times cn is repeating for each dn.. i can feed in the attribute name manually so it does not have to magically find all attributes and count them.. So can pipe the file into some awk or something with mentioning dn , cn, sn and it will print the above
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $dn;
my %H ;
while (<>) {
# print; # debug
chomp;
/^dn:/ and $dn = $_;
next unless defined $dn;
$H{$dn}->{count}++ if /^cn:/;
}
print Dumper(\%H);
You may try another language too, I do not know which one do you prefer (awk/python/perl/whatever).
Here is another approach (pseudocode too)
Code:
if line starts with dn:
new record
ix += 1 (index, counter of dn's)
else
split line to name/value pairs
store name/value in record[ix] (this looks like hash or associative array)
finally count the number of elements in the hash (or count number of occurrences of something or ...)
By the way what will/should happen if you have two identical lines next to each other?
haha! I enjoy a bit of nifty Perl! I am trying to avoid doing a conformance test today anyway
Code:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $dn;
my %H ;
my $thing;
while (<>) {
print;
chomp;
next unless /./;
if (m/^dn:/) {
$dn = $_;
next;
}
next unless defined $dn;
next unless ($thing) = /^(\w+):/;
$H{$dn}->{$thing}++ ;
}
print Dumper(\%H);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.