# Printing from multiline output

**URL:** <https://community.unix.com/t/printing-from-multiline-output/376219>\
**Category:** Shell Programming and Scripting\
**Created:** [September 5, 2019, 6:23am UTC](https://community.unix.com/t/printing-from-multiline-output/376219 "2019-09-05T06:23:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baluchen](https://community.unix.com/letter_avatar/baluchen/32/6_449c533f586939b6e5f246632a46421f.png) [@baluchen](https://community.unix.com/u/baluchen)\
**Post date:** [September 5, 2019, 6:23am UTC](https://community.unix.com/t/printing-from-multiline-output/376219/1 "2019-09-05T06:23:05Z")

</div>

Dear all,

I have below "ldapsearch" output

```nohighlight
[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.

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

```

So that final output should looks like

```nohighlight
uid : displayName : street (after base64 decode)

```

Example:

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

```

Thanks  
Bala

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [September 5, 2019, 6:26am UTC](https://community.unix.com/t/printing-from-multiline-output/376219/2 "2019-09-05T06:26:06Z")

</div>

... 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?

---

<div class="post-metadata">

**Author:** ![baluchen](https://community.unix.com/letter_avatar/baluchen/32/6_449c533f586939b6e5f246632a46421f.png) [@baluchen](https://community.unix.com/u/baluchen)\
**Post date:** [September 5, 2019, 6:43am UTC](https://community.unix.com/t/printing-from-multiline-output/376219/3 "2019-09-05T06:43:55Z")

</div>

> [@neo](#):
>
> ... 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.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [September 5, 2019, 7:38am UTC](https://community.unix.com/t/printing-from-multiline-output/376219/4 "2019-09-05T07:38:21Z")

</div>

So you question is:

> [@](#):
>
> How to convert one text with Base64 decode and print the other two texts with the desired format?

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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 5, 2019, 12:19pm UTC](https://community.unix.com/t/printing-from-multiline-output/376219/5 "2019-09-05T12:19:57Z")

</div>

Like so?

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 5, 2019, 2:59pm UTC](https://community.unix.com/t/printing-from-multiline-output/376219/6 "2019-09-05T14:59:56Z")

</div>

With bash builtins:

```nohighlight
#!/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 ...)`

---

<div class="post-metadata">

**Author:** ![baluchen](https://community.unix.com/letter_avatar/baluchen/32/6_449c533f586939b6e5f246632a46421f.png) [@baluchen](https://community.unix.com/u/baluchen)\
**Post date:** [September 6, 2019, 10:51am UTC](https://community.unix.com/t/printing-from-multiline-output/376219/7 "2019-09-06T10:51:19Z")

</div>

> [@madeingermany](#):
>
> With bash builtins:
> 
> ```plaintext
> #!/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 ...)`

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?

```plaintext
[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

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 6, 2019, 1:03pm UTC](https://community.unix.com/t/printing-from-multiline-output/376219/8 "2019-09-06T13:03:39Z")

</div>

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.
