5

By writing an ldapsearch command such as:

ldapsearch -h ipaddress -p 389 -D "cn=func_01_acc,ou=admins,dc=akademia,dc=int" \
-w akademia.01 -b "ou=stud01,dc=akademia,dc=int" "(l=Torun)" sn cn telephonenumber -LLL |
grep sn: | awk '{print $2 "|" $1}' | sort | uniq -u 

I got output:

 dn: uid=s21705,ou=stud01,dc=akademia,dc=int
 telephoneNumber: +78123793414
 cn: Benny Flowers

 dn: uid=s21711,ou=stud01,dc=akademia,dc=int
 telephoneNumber: +78123439058
 cn: Jacqueline Newton

The question is: how to format it to

Benny Flowers|+78123793414
JAcqueline Newton|+78123439058

The farest I went was:

ldapsearch -h ipaddress -p 389 -D "cn=func_01_acc,ou=admins,dc=akademia,dc=int" \
-w akademia.01 -b "ou=stud01,dc=akademia,dc=int" "(l=Torun)" sn cn telephonenumber -LLL |
grep sn: | grep  dn: | sed 's/telephoneNumber: //' | sed 's/cm: //'

And I have:

 +123123123
 Name Surname

 +123123123
 Name Surname

I tried to invert number and names with:

 for ((i=0;i<$elements;i++)); do
      echo ${array[${i}]}
 done

It printed output, but I cannot find out how to pass value to temporary array for names and for another array handling numbers.

Any suggestions would be helpful.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
J. Doe
  • 51
  • 1
  • 2
  • 1
    Post the ldap output before you do the `grep sn: | awk '{print $2 "|" $1}' | sort | uniq -u` and we can help you. I don't understand how you can get the output you say you do though after a pipe that ends in `sort | uniq -u`. – Ed Morton Feb 20 '16 at 23:52

3 Answers3

2

If you pipe your first output to:

sed -rn '/ telephoneNumber: /{N;s/[^:]*: (.*)\n[^:]*: (.*)/\2|\1/p;}'

You will get:

Benny Flowers|+78123793414
Jacqueline Newton|+78123439058

Use -En instead on OSX.

sed will search each telephoneNumber entries, N will place that and the following line together, () will match the name and the phone number and finally \2|\1 formats the resulting string.

Joao Morais
  • 1,885
  • 13
  • 20
2

awk

$ awk -v OFS='|' '{split($0,a,": ")} /^telephoneNumber:/{tel=a[2]} /^cn/{cn=a[2]; print cn, tel}' ldapoutput.txt 
Benny Flowers|+78123793414
Jacqueline Newton|+78123439058

shell

$ cat foo.sh 
#!/bin/bash

while IFS=':' read -r key value; do
        case ${key} in
                cn|telephoneNumber)
                        read -r "${key}" <<<"${value## }" ;;
                *)
                        continue
                        ;;
        esac
        [ "${key}" = cn ] && printf "%s|%s\n" "${cn}" "${telephoneNumber}"
done

.

$ ./foo.sh < ldapoutput.txt
Benny Flowers|+78123793414
Jacqueline Newton|+78123439058
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
0
grep -E '^(telephoneNumber|cn):' | cut -d':' -f2 | sed 's#\s##' | sed -r '$!N;s/(.*)\n(.*)/\2|\1/'
Wanming Zhang
  • 323
  • 1
  • 8