Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用字符串替换命令结果bash中的行_Bash_Email_Ldap - Fatal编程技术网

用字符串替换命令结果bash中的行

用字符串替换命令结果bash中的行,bash,email,ldap,Bash,Email,Ldap,我有一个包含用户和IP列表的文件,标签分开。在这个文件中,我想用ldapsearch查询替换我获得的用户邮件的用户名 我设法在一个很好的用户和邮件列表上获得了ldapsearch结果,而且也是以标签分隔的 在bash中最简单的方法是什么 样本输入: 文件 name1<tab>ip1<tab>other stuff 1 name2<tab>ip2<tab>other stuff 2 ... 所需输出(转换文件) mail1ip1其他内容1 邮件2其

我有一个包含用户和IP列表的文件,标签分开。在这个文件中,我想用
ldapsearch
查询替换我获得的用户邮件的用户名

我设法在一个很好的用户和邮件列表上获得了ldapsearch结果,而且也是以标签分隔的

在bash中最简单的方法是什么

样本输入

文件

name1<tab>ip1<tab>other stuff 1
name2<tab>ip2<tab>other stuff 2
...
所需输出(转换文件)

mail1ip1其他内容1
邮件2其他资料2
...

您可以使用
加入

$ 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
正常工作。如果不是,则可以使用如下构造:

$ join <(sort ldapoutput) <(sort file)

$join您可以使用
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
正常工作。如果不是,则可以使用如下构造:

$ join <(sort ldapoutput) <(sort file)

$join您可以使用此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$

您可以使用此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$