I've a file that contains entries like this:

ddn: cn=312548,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312548001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
member: cn=312548999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
abcName: something else

dn: cn=312544,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312544001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
member: cn=312544999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
abcName: after bike

dn: cn=312542,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312542001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
member: cn=312542999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
abcName: pen mark

dn: cn=312539,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312539999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
member: cn=312539001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
abcName: used car

dn: cn=312537,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312537001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
member: cn=312537999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
abcName: Book bike

dn: cn=312534,ou=Entities,ou=Active,ou=Vault,o=abc
member: cn=312534999,ou=ServiceAccounts,ou=Active,ou=Vault,o=abc
member: cn=312534001,ou=Unnamed,ou=Identities,ou=Active,ou=Vault,o=abc
member: cn=fe7316f7-05d6-439b-8b9b-4364f4936615,ou=Named,ou=Identities,ou=Acti
abcName: Apple

Now I want to know only the dn entries which have a member "Named".

The output should contain the "abcName" and the "dn".

  • Please see the editing guidelines and edit your post accordingly. – lcd047 Jun 22 '15 at 9:45
  • which entry in your example should be printed ? Could you show an example of the output you desire as well. – X Tian Jun 22 '15 at 9:55

On your sample,

awk -vRS= -F': *|\n' '/,ou=Named,/{print $2, $NF}' < file

would output:

cn=312534,ou=Entities,ou=Active,ou=Vault,o=abc Apple

(that assumes abcName is the last attribute in each entry and that ,ou=Named, may not be found in other attributes or the dn, and that the attribute values are not encoded).

To account for LDIF line continuations (where for instance ou=Named could become ou=Na\n med), you can first remove them:

awk -vRS= -F': *|\n' '{gsub(/\n /, "")}; /,ou=Named,/{print $2, $NF}' < file
  • 1
    LDIF can have based64 encoded text, too. Parsing this stuff is horrible. (I know.) Probably easier to ignore that and wait to see if it's a problem to the OP – roaima Jun 22 '15 at 10:06
  • 1
    @roaima, or use perl -MNet::LDAP::LDIF – Stéphane Chazelas Jun 22 '15 at 10:07
  • Whats GNU in my answer ?(sorry can't ping you on mine) – 123 Jun 22 '15 at 11:02
  • @User112638726, check the edit log. Note that you can always ping editors (though their name are not offered for completion). – Stéphane Chazelas Jun 22 '15 at 11:29
  • @StéphaneChazelas Didn't really understand what you meant ? And i didn't know that thanks :) – 123 Jun 22 '15 at 11:32

With GNU sed

sed 'H;/^$/{h};/abcName/{x;/,ou=Named,/{s/member:[^\n]*\n//g;p}};d' file

dn: cn=312534,ou=Entities,ou=Active,ou=Vault,o=abc
abcName: Apple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.