问题
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.
回答1:
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.
回答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
回答3:
grep -E '^(telephoneNumber|cn):' | cut -d':' -f2 | sed 's#\s##' | sed -r '$!N;s/(.*)\n(.*)/\2|\1/'
来源:https://stackoverflow.com/questions/35529922/filter-ldapsearch-with-awk-bash