Thursday, November 18, 2021

[SOLVED] RegEx: Get multiline LDAP entry

Issue

I'm trying to capture an entire LDAP entry from dn:.+ to the entry's last line, but stopping at last line before next entry, e.g., \n#entry-id: 8266. My trial and error using egrep is getting absolutely nowhere. NOTE: I'm using exported ldif files where the data resides, fwiw.

Closest I've come is with egrep "dn: cn=name,ou=People,dc=example,dc=com.+.|\n*.+\n" but no output on terminal. I've tested the actual regex on regexr.com. I understand that is a completey different env.

Thanks in advance!


Sample Data:


dn: cn=name,ou=People,dc=example,dc=com \
shadowLastChange: 17492 \
userPassword: password \
sn: Last \
givenName: First \
cn: first \
mail: [email protected] \
displayName: First Last \
o: University \
ou: Dept. \
objectClass: top \
objectClass: person

\# entry-id: 8266

Solution

With your shown samples, please try following awk code.

awk '/entry-id/{found=""} /^dn:/{found=1} found' Input_file

OR in case you want to print only 1 set from dn: before entry-id: then try following code:

awk '/entry-id/{exit} /^dn:/{found=1} found' Input_file


Answered By - RavinderSingh13