Printing from multiline output

Dear all,

I have below "ldapsearch" output

[v00000@10-119-225-51 ~]$ ldapsearch -D "CN=SVC-ACCOUNT,OU=SVC,OU=VDSI,OU=Non-Human,DC=Org,DC=com" -w secretword -H ldaps://org.com:636 -b DC=Org,DC=com -s sub uid=v00000 "(filter)" "displayName" "uid" "street"
# extended LDIF
#
# LDAPv3
# base <DC=Org,DC=com> with scope subtree
# filter: uid=v00000
# requesting: (filter) displayName uid street
#

# v00000, Org.com
dn: CN=v00000,OU=Accounts,DC=Org,DC=com
street:: UlBKRCwgQUJDIEJ1aWxkaW5nIDEwMCBDYW1wdXMtOSxEci4gS2FtYXRjaGkgUm9hZApOb3J0aCBWZWVyYW5hbSBTYWxhaSxQZXJ1bmd1ZGkgVmlsbGFnZQpUYW1pbG5hZHU=
displayName: Thangamani, Balamurugan
uid: v00000

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

I would like to print as below
The street value should be decoded with Base64 format using below command.

openssl enc -base64 -d <<< "UlBKRCwgQUJDIEJ1aWxkaW5nIDEwMCBDYW1wdXMtOSxEci4gS2FtYXRjaGkgUm9hZApOb3J0aCBWZWVyYW5hbSBTYWxhaSxQZXJ1bmd1ZGkgVmlsbGFnZQpUYW1pbhZHU="

So that final output should looks like

uid : displayName : street (after base64 decode)

Example:

v00000     :      Thangamani, Balamurugan      :        RPJD, ABC Building 100 Campus-9,Dr. Kamatchi Road, North Veeranam Salai,Perungudi Village Tamilnadu

Thanks
Bala

... and what is your exact question and what code have you tried / attempted / written to accomplish your ask?

... and if you did attempt to write your own code, what were the results, error messages, etc?

... and if you did not attempt to write your own code, why not?

I want to display 3 different texts from the output which are not properly formated. I tried "|awk '/displayName/ {print $0}'" and i can filter one output at a time. So the ask is how to convert one text with Base64 decode and printer other two texts with desired format.

So you question is:

The easy approach is to store each of the three texts / tokens you want to print in variables and then print a single string formatted as you wish.

I always process text like this in PHP (because I find PHP easier to read and maintain months and years after I write the scripts, but that's just me), so now that you have make it more clear what you want, I'll leave this to some of the great coders here who are into awk .

Thanks for clarifying.

Like so?

awk -F:  '
/^(#| *$|search|result)/        {next
                                }
NF == 3                         {gsub (/^ | $/, "", $3)
                                 CMD = sprintf ("base64 -d << EOF\n" $3 "\nEOF\n")
                                 $3 = ""
                                 while (1 == CMD | getline X) $3 = $3 "\t" X
                                }
1
' OFS=: file
dn: CN=v00000,OU=Accounts,DC=Org,DC=com
street::    RPJD, ABC Building 100 Campus-9,Dr. Kamatchi Road    North Veeranam Salai,Perungudi Village    Tamilnadu
displayName: Thangamani, Balamurugan
uid: v00000
2 Likes

With bash builtins:

#!/bin/bash
while read w1 line
do
  case $w1 in
  (uid:) uid=$line;;
  (displayName:) displayName=$line;;
  (street::) street=$(openssl enc -base64 -d <<< "$line" | tr '\n' ',');;
  esac
done < file
printf "%10s : %30s : %40s\n" \
"uid" "displayName" "street" \
"$uid" "$displayName" "$street"

You can replace < file with < <(ldapsearch ...)

1 Like

thank you very much. just one more tweaking is required. The street value sometimes has more than 1 line. how to capture if it contains two line as seen below?

[v00000@10-119-225-51 ~]$ ldapsearch -D "CN=SVC-ACCOUNT,OU=SVC,OU=VDSI,OU=Non-Human,DC=Org,DC=com" -w secretword -H ldaps://org.com:636 -b DC=Org,DC=com -s sub uid=v00000 "(filter)" "displayName" "uid" "street"
# extended LDIF
#
# LDAPv3
# base <DC=Org,DC=com> with scope subtree
# filter: uid=v00000
# requesting: (filter) displayName uid street
#

# v00000, Org.com
dn: CN=v00000,OU=Accounts,DC=Org,DC=com
street:: UlBKRCwgQUJDIEJ1aWxkaW5nIDEwMCBDYW1wdXMtOSxEci4gS2FtYXRjaGkgUm9hZApOb3J0aCBWZWVyYW5hbSBTYWxhaSxQZXJ1bmd1ZGkgVmlsbGFnZQpUYW1pbG5hZHU=
             UlBKRCwgQUJDIEJ1aWxkaW5nIDEwMCBDYW1wdXMtOSxEci4gS2FtYXRjaGkgUm9hZApOb3J0aCBWZWVyYW5hbSBTYWxhaSxQZXJ1bmd1ZGkgVmlsbGFnZQpUYW1pbG5hZHU=
displayName: Thangamani, Balamurugan
uid: v00000

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

You mean the base64-string is repeated on the next line?
Doesn't make sense, does it? Currently it is ignored. Nothing to be done.