다음과 같은 항목이 포함된 파일이 있습니다.
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
ve,ou=Vault,o=abc
abcName: Apple
이제 "Named" 멤버가 포함된 dn 항목만 알고 싶습니다.
출력에는 "abcName" 및 "dn"이 포함되어야 합니다.
답변1
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
답변2
귀하의 샘플에서,
awk -vRS= -F': *|\n' '/,ou=Named,/{print $2, $NF}' < file
출력됩니다:
cn=312534,ou=Entities,ou=Active,ou=Vault,o=abc Apple
( abcName
각 항목의 마지막 속성으로 간주되며 ,ou=Named,
다른 속성에서 찾을 수 없거나 dn
속성 값이 인코딩되지 않습니다.)
ou=Named
LDIF 행 연속( 예: ) 을 고려하려면 ou=Na\n med
먼저 제거할 수 있습니다.
awk -vRS= -F': *|\n' '{gsub(/\n /, "")}; /,ou=Named,/{print $2, $NF}' < file