替换文件中的 base64 值

替换文件中的 base64 值

我有一个文件,它从 ldapsearch 命令获取详细信息并创建文件,如下所示

# 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

如果你看到SN::&给定的名称::有时有 base64 值,我想用命令解码它,但我无法做到

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

我如何只能解码 sn:: 和给定名称:: 具有 base64 值并再次保存到同一文件。

请帮忙,武士

答案1

使用任何 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é

它会为每次调用分离出一个子 shell,base64因此该部分会很慢。

答案2

使用 perl 及其MIME::Base64模块中,以下一个衬垫对字段名称以两个冒号 (::) 结尾的每一行的数据进行 base64 解码。其他线路保持不变。

MIME::Base64是一个核心 Perl 模块,自 v5.8(2002 年 7 月)起就包含在 Perl 中。

$ 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é

这会打印到标准输出。要使其修改输入文件,您可以使用 perl 的-i就地编辑选项。

相关内容