mba777 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys

i have some strings who looks like that:

CN=ext.Group.eso,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local

or that:

CN=ext.Group.test,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local

Is it possible to get only the term "eso" and "test" out of these strings? What i want to say i need this term:

CN=ext.Group."MY TERM",OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local

Thanks Andy

Replies are listed 'Best First'.
Re: Substr Perl
by choroba (Cardinal) on Nov 26, 2013 at 11:02 UTC
    If the wanted string will always be preceded by "CN=ext.Group.", you can use the following regular expression:
    for my $string ('CN=ext.Group.eso,OU=External,OU=ExDistriGroups,OU=Acc +ounts,DC=eso,DC=local', 'CN=ext.Group.test,OU=External,OU=ExDistriGroups,OU=Ac +counts,DC=eso,DC=local', 'CN=ext.Group."MY TERM",OU=External,OU=ExDistriGroups, +OU=Accounts,DC=eso,DC=local', ) { my ($term) = $string =~ /CN=ext\.Group\.([^,]+)/; print "Found: $term\n"; }

    See perlre and perlop for details.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      thats exactly what i need, but how does it work if the list of "CN=...." is an array?

        Can you give us an example of what such a record would look like?

        It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Substr Perl
by boftx (Deacon) on Nov 26, 2013 at 11:41 UTC

    If I understand you correctly, you want to extract the "CN" term from the record string. Here is a somewhat brute-force approach but I think it demonstrates the principle you need to see.

    # assuming you have a record string in $record and the field order is # always the same. my @fields = split(/\,/, $record); my $group = $fields[0]; # do this if you need to strip the "CN=" from the field $group =~ s/^CN=//; print $group . "\n";

    Given this and your prior question about storing multiple values in a single DB column, I think you should read up on Perl regex, split and join as well as other basic text processing functions. Perl excels at this kind of work, but you need to know what tools you have available.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      sorry for my stupid questions but i'm actually no programmer ;), For example i have something like that:
      my @member = $entry->get_value ('memberOf'); my @memberof = grep(/\CN=ext\.Group\.([^,]+)/,@member); $member = join(',',@memberof); print "$uid -- $member\n";
      This code gives me the following output: Michael Douglas -- CN=ext.Group.eso,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local,CN=ext.Group.test,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local,CN=ext.Group.test1,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local,CN=ext.Group.test2,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local What i want is: Michael Douglas -- eso, test, test1, test2 Thanks Andy
        map instead of grep was the solution for me, thank you hdb !! Tanks to all ;)
Re: Substr Perl
by jellisii2 (Hermit) on Nov 26, 2013 at 12:58 UTC
    Might I recommend that you use an actual LDAP tool/module to manipulate what very much appears to be an LDIF file?
Re: Substr Perl
by marto (Cardinal) on Nov 26, 2013 at 11:06 UTC

    Update:It seems I've misread your request, you do not want to replace anything. Ignore the answer below.

    "Is it possible to get only the term "eso" and "test" out of these strings? What i want to say i need this term: CN=ext.Group."MY TERM",OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC=local"

    Your specification isn't quite right, if you replace "eso" with "my term" you'll break the DC part of your LDAP connection string.

    Specify a groupname, identify the strings you wish to replace e.g. "Group.eso", rather than all the "eso"s, use a regex to replace them:

    #!/usr/bin/perl use strict; use warnings; my $groupname = "derp"; my %replaceGroups = ( "Group.test" => "Group.$groupname", "Group.eso" => "Group.$groupname", ); my $regex = join "|", keys %replaceGroups; $regex = qr/$regex/; # both cnstrings for testing. my $cnstring = "CN=ext.Group.eso,OU=External,OU=ExDistriGroups,OU=Acco +unts,DC=eso,DC=local"; #my $cnstring = "CN=ext.Group.test,OU=External,OU=ExDistriGroups,OU=Ac +counts,DC=eso,DC=local"; $cnstring =~ s/($regex)/$replaceGroups{$1}/g; print "$cnstring\n";

    Output:

    CN=ext.Group.derp,OU=External,OU=ExDistriGroups,OU=Accounts,DC=eso,DC= +local

    Update: fixed spelling mistake.