我有一个文件,其中包含用户和ip的列表,以制表符分隔。我想在这个文件中用ldapsearch
查询来代替我获得的用户的邮件的用户名。
我设法在一个很好的用户和邮件列表上获得了ldapsearch结果,这些列表也是制表符分隔的。
在bash中,最简单的方法是什么?
样本输入 :
文件
name1<tab>ip1<tab>other stuff 1
name2<tab>ip2<tab>other stuff 2
...
Ldap输出
name1 (mail1)
name2 (mail2)
...
所需输出(转换文件)
mail1<tab>ip1<tab>other stuff 1
mail2<tab>ip2<tab>other stuff 2
...
您可以使用join
:
$ cat file
name1 ip1 other stuff 1
name2 ip2 other stuff 2
$ cat ldapoutput
name1 (mail1)
name2 (mail2)
$ join <(tr -d '()' < ldapoutput) file | cut -d ' ' -f 2-
mail1 ip1 other stuff 1
mail2 ip2 other stuff 2
以下更简单的版本会在每行上留下名称:
$ join ldapoutput file
name1 (mail1) ip1 other stuff 1
name2 (mail2) ip2 other stuff 2
请注意,必须对输入文件进行排序,join
才能工作。如果不是,您可以使用这样的构造:
$ join <(sort ldapoutput) <(sort file)
您可以使用这个awk一行:
awk -F '[ t()]' 'FNR==NR {a[$1]=$3;next} $1 in a{$1=a[$1]}1' OFS='t' ldap file
mail1 ip1 other stuff 1
mail2 ip2 other stuff 2
使用管道与cat -vte
:
mail1^Iip1^Iother^Istuff^I1$
mail2^Iip2^Iother^Istuff^I2$