Convert to lower case based on pattern

Hi Gurus,

I am trying to convert some lines in a file based on the patter.Below is an example. Text after cn= and uid: should be converted to lower case.

Input:
dn: cn=XXX,ou=111,dc=222,dc=333,dc=444
uid: XXX
userPassword:: aAbVCeDr

dn: cn=XYZ,ou=111,dc=222,dc=333,dc=444
uid: XYZ
userPassword:: aAbVCesdsaDr

dn: cn=ABC,ou=111,dc=222,dc=333,dc=444
uid: ABC
userPassword:: aAbVzcxxvcxCeDr
output:
dn: cn=xxx,ou=111,dc=222,dc=333,dc=444
uid: xxx
userPassword:: aAbVCeDr

dn: cn=xyz,ou=111,dc=222,dc=333,dc=444
uid: xyz
userPassword:: aAbVCesdsaDr

dn: cn=abc,ou=111,dc=222,dc=333,dc=444
uid: abc
userPassword:: aAbVzcxxvcxCeDr

Thanks for the help.
Samingla

What have you tried so far?

nawk 'BEGIN{FS=OFS="cn="} t=tolower($1); print t,$2}' input.txt
awk 'BEGIN{FS=OFS=","} /cn=/{uid=substr($1,index($1,"=")+1)}
/cn=|uid/{sub(uid,tolower(uid),$1)} 1 ' input.txt
1 Like

Hello,

Following may also help.

awk -F"\:|\," '/cn/ {$2=tolower($2); OFS=","}; /uid\:/ {$2=tolower($2);OFS=":"} 1' check_lower_case

Output will be as follows.

Input:
dn, cn=xxx,ou=111,dc=222,dc=333,dc=444
uid: xxx
userPassword:: aAbVCeDr
dn, cn=xyz,ou=111,dc=222,dc=333,dc=444
uid: xyz
userPassword:: aAbVCesdsaDr
dn, cn=abc,ou=111,dc=222,dc=333,dc=444
uid: abc
userPassword:: aAbVzcxxvcxCeDr

Thanks,
R. Singh

1 Like

Hi Singh,

Thanks for the update. I see that the empty line is missing after each section of records.

Thanks,
Sam