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

Hi, I am trying to write a perl script, I am stuck while trying to put a variable from a file search, my file looks like this.. dn: ou=CA, ou=abc, o=com objectClass: top objectClass: organizationalUnit ----- ---- I want to search for first occurence of "dn:" and then pull out the value "CA" and put it in a variable. Please help me to accomplish this. Regards, Scott

Replies are listed 'Best First'.
Re: Help Needed
by kirillm (Friar) on Feb 03, 2009 at 11:59 UTC

    Seems like your file is a LDIF file. You can use Net::LDAP::LDIF to deal with it.

    A question: does your file contain multiple occurences of "dn:"?

    Kirill

Re: Help Needed
by rovf (Priest) on Feb 03, 2009 at 12:00 UTC

    Is it always dn: ou=CA, or what can be in between dn and CA? Assuming that you read the file line by line, something like

    my $value=undef; open(FILE,'<',$filename) or die "$filename: $!"; while(<FILE>) { if(/\bdn:\s*ou=(\w+)/) { $value=$1; last; } }
    could do.

    -- 
    Ronald Fischer <ynnor@mm.st>
      Hi, RE: My file contains several occurence of "dn:" and I am only concerned with the first occurence and it will not be always dn: ou=CA, the value CA might change. Regards, Scott

        Of course your value of CA will change, otherwise you wouldn't have to extract it! But what will be the general pattern between dn and CA? Is it always ': ou=' or something different?

        But in any case, with the solution I posted, I think you now know how to do it...

        -- 
        Ronald Fischer <ynnor@mm.st>