0

I have one file which is gets details from ldapsearch command and create file as below

# lschuler, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=lschuler,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/lschuler
gidNumber: 10000
uid: lschuler
cn: Leonie Schuessler
uidNumber: 20056
mail: [email protected]
sn: Schuessler
givenName: Leonie

# cadelie, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=cadelie,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/cadelie
gidNumber: 10000
uid: cadelie
cn:: Q2hsb8OpIEFkw6lsaWU=
uidNumber: 20057
mail: [email protected]
sn:: QWTDqWxpZQ==
givenName:: Q2hsb8Op

if you see sn:: & givenName:: some times have base64 value I want to decode it with command which I am not able to do

cat file.text | sed -e "s/.*sn:: //g;s/;.*//" |base64 -d && cat file.text | sed -e "s/.*givenName:: //g;s/;.*//" |base64 -d

how I can decode only sn:: & givenName:: which has base64 value and save to same file again.

Please help, SAMURAI

2
  • Seems a lot like this previous Replace a base64 value in a file in unix Commented Feb 17, 2023 at 12:52
  • Yes but in my case there are multiple entries with base64, I was able to decode that but cat file.text | sed -e "s/.*sn:: //g;s/;.*//" |base64 -d && cat file.text | sed -e "s/.*givenName:: //g;s/;.*//" |base64 -d with this but not able to save decoded value to respective field , its is getting stored in every SN and GivenName with first value only Commented Feb 20, 2023 at 8:48

2 Answers 2

1

Using any awk:

$ cat tst.awk
/^(sn|givenName):: */ {
    tag = val = $0
    sub(/ .*/,"",tag)
    sub(/^[^ ]+ */,"",val)
    cmd = "printf \047%s\047 \047" val "\047 | base64 -d"
    if ( (cmd | getline line) > 0 ) {
        $0 = tag " " line
    }
    close(cmd)
}
{ print }

$ awk -f tst.awk file
# lschuler, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=lschuler,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/lschuler
gidNumber: 10000
uid: lschuler
cn: Leonie Schuessler
uidNumber: 20056
mail: [email protected]
sn: Schuessler
givenName: Leonie

# cadelie, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=cadelie,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/cadelie
gidNumber: 10000
uid: cadelie
cn:: Q2hsb8OpIEFkw6lsaWU=
uidNumber: 20057
mail: [email protected]
sn:: Adélie
givenName:: Chloé

It's spinning off a subshell for every call to base64 so that part will be slow.

0
1

Using perl and its MIME::Base64 module, the following one liner base64-decodes the data from every line with a field name ending in two colons (::). Other lines are passed through unchanged.

MIME::Base64 is a core perl module, included with perl since v5.8 (July 2002).

$ perl -MMIME::Base64 -pe 's/^((?:[^:]*)):: *(.*)/"$1: " . decode_base64($2)/e' file.txt 
# lschuler, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=lschuler,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/lschuler
gidNumber: 10000
uid: lschuler
cn: Leonie Schuessler
uidNumber: 20056
mail: [email protected]
sn: Schuessler
givenName: Leonie

# cadelie, people, pl.s2-eu.XXXXXXXXX.local
dn: uid=cadelie,ou=people,dc=pl,dc=s2-eu,dc=XXXXXXXXX,dc=local
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/cadelie
gidNumber: 10000
uid: cadelie
cn: Chloé Adélie
uidNumber: 20057
mail: [email protected]
sn: Adélie
givenName: Chloé

This prints to stdout. To make it modify the input file, you can use perl's -i in-place edit option.

3
  • Yes this also works, but I want to run those command in docker container which doesn't have perl installed so looking for awk or sed examples Commented Feb 20, 2023 at 12:43
  • So, install perl in the container. It's not exactly difficult to do so (and lots of docker base images include perl anyway, or have a distro package for it at least), nor does it consume significant amounts of disk space. More than awk or sed, certainly, with all its library modules but they allow it to do a lot more than either (especially sed), and with a lot less effort. Commented Feb 20, 2023 at 15:45
  • Sure Cas, thank you for your support and time, as I mentioned its working perfect for me only the issue is I dont have access to build docker images to add perl. I really appreciate your support. Commented Feb 21, 2023 at 6:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.