Filter ldapsearch with awk/bash
Asked Answered
L

3

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.

Liquefy answered 20/2, 2016 at 22:34 Comment(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.Alcohol
G
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.

Guiscard answered 20/2, 2016 at 23:46 Comment(0)
A
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
Assignor answered 22/2, 2016 at 12:10 Comment(0)
H
0
grep -E '^(telephoneNumber|cn):' | cut -d':' -f2 | sed 's#\s##' | sed -r '$!N;s/(.*)\n(.*)/\2|\1/'
Heindrick answered 21/2, 2016 at 0:1 Comment(1)
Maybe a little comment as to how and why this works?Prink

© 2022 - 2024 — McMap. All rights reserved.