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
네가 본다면전화번호::&이름::가끔 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é
각 호출에 대해 하위 쉘을 분리하므로 base64
해당 부분이 느려집니다.
답변2
Perl과 그 도구를 사용하여MIME::Base64모듈에서 다음 패드는 필드 이름이 두 개의 콜론(::)으로 끝나는 각 행의 데이터를 base64로 디코딩합니다. 다른 줄은 변경되지 않습니다.
MIME::Base64
v5.8(2002년 7월)부터 Perl에 포함된 핵심 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
내부 편집 옵션을 사용할 수 있습니다.