20110106 regex to trim objectclasses - plembo/onemoretech GitHub Wiki

title: regex to trim objectclasses link: https://onemoretech.wordpress.com/2011/01/06/regex-to-trim-objectclasses/ author: lembobro description: post_id: 85 created: 2011/01/06 12:49:56 created_gmt: 2011/01/06 12:49:56 comment_status: open post_name: regex-to-trim-objectclasses status: publish post_type: post

regex to trim objectclasses

Here’s another neat trick: a perl regex to trim objectclass values from an LDIF. I had to do this to shoehorn some entries into a directory server that didn’t like a particular combination of object classes in a set of entries.

First, an example entry:

dn: cn=Acme Corporation,ou=companies,ou=reference,dc=example,dc=com
objectclass: top
objectclass: acmestandard
objectclass: acmecompany
cn: Acme Corporation
acmeregion: North America

The problem here was the acmestandard object class. For historical reasons all objects on my company's Sun Directory under the “ou=reference” container has this object class in addition to others. There were a couple of hundred business organization objects in there, used for reference purposes by various web applications. That was OK so long as the data resided on Sun, but going over to OpenLDAP with its stricter schema rules was a train wreck (note "acme" is a fictitious name, my company's real name prefixes all custom objects I've created in its schema -- each with its own unique oid value, of course).

The solution was to just remove the acmestandard object class from each of these entries.

How to do that seemed simple, just use a perl one-liner to “edit” the LDIF file being used to load the data.

After several trials and errors, and reference to this often humorous e-nor article, I had my answer:

**

perl -0777 -pi -e 's/objectclass: acmestandardnobjectclass: acmecompany/objectclass: acmecompany/gis;' companies.ldif

**

There’s a lot that goes into this handy little command string. Key differences with the usual single-line “edit” method are the use of the “-0777” switch to make perl act on the whole target file at once, rather than line by line, and the lack of any line termination indicator (”$”). The “s” modfier at the end tells the script act as if the search text was all on one line (g and i are for “global” or “greedy” and “case-_in_sensitive”, respectively), and to allow the use of either “.” or “n” for embedded newlines (my preference is to use “n” — using “.” in a script this powerful scares me too much) .

Copyright 2004-2019 Phil Lembo