用awk/bash过滤ldapsearch [英] Filter ldapsearch with awk/bash

查看:81
本文介绍了用awk/bash过滤ldapsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过编写ldapsearch命令,例如:

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 

我得到了输出:

 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

我走得最远的是:

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: //'

我有:

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

推荐答案

如果将第一个输出传递给:

If you pipe your first output to:

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

您将获得:

Benny Flowers|+78123793414
Jacqueline Newton|+78123439058

在OSX上改用-En.

sed将搜索每个telephoneNumber条目,N将其与下一行放在一起,()将匹配名称和电话号码,最后\2|\1格式化结果字符串.

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.

这篇关于用awk/bash过滤ldapsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆