in reply to Perl File Manipulation

If the data format is consistent in terms of always having at least one blank line between consecutive records, and never having a blank line within a record, then you can set the INPUT_RECORD_SEPARATOR to read one whole record at a time into $_, and this can simplify things a lot:
{ local $/ = ""; # set to empty string -- see perldoc perlvar while (<>) { if ( /(\ncn: (\S+))\n(\S+)/ ) { my ( $cnline, $cnval, $nexttag ) = ( $1, $2, $3 ); if ( $nexttag ne "uid" ) { s/$cnline/$cnline\nuid: $cnval/; } } else { warn "Record $. has no cn value\n"; } print; } }
(not tested)

Replies are listed 'Best First'.
Re^2: Perl File Manipulation
by wfsp (Abbot) on Jul 09, 2004 at 18:03 UTC
    The line:
    if ( $nexttag ne "uid" ) {
    needs to be:
    if ( $nexttag ne "uid:" ) {
    (tested!)
    Picky or what!
    I'm not familiar with the 'LDAP Data Interchange Format' (to say the least) but is 'uid' always bound to follow 'cn'?
    wfsp
      Thanks for the fix (and the test).
      I'm not familiar with the 'LDAP Data Interchange Format' (to say the least) but is 'uid' always bound to follow 'cn'?

      I don't know either, but if it varies, reading the data one whole record at a time will make it a lot easier to handle the variation.

        Hey guys,
        Thanks for all the help, I've almost got it working now. UID is not a necessary attribute of an LDAP record, however to use Oracle Portal it is. I'm trying to move from a Oracles 902 ldap directory to a 10g directory and this is my final step.

        My only problem now is I have 4 records with cn on the last line of the record and for some reason its not find it in the search. i.e.

        cn=HELPDESK,cn=users,dc=myco,dc=com
        orclactivestartdate: 20031202043257Z
        objectclass: top
        objectclass: person
        objectclass: inetOrgPerson
        objectclass: organizationalPerson
        objectclass: orclUser
        objectclass: orclUserV2
        o: MyOrg
        orcldefaultprofilegroup: cn=user_grp,cn=portal_groups,cn=groups,dc=myco,dc=com
        givenname: Joe
        sn: Schmo
        telephonenumber: 800-999-9999
        userpassword: {MD4}1a4QEWWP+nCZ19uAy9w==
        mail: joe_blow@myco.com
        cn: HELPDESK
        Any ideas? Thanks again Matt