用字符串替换命令结果bash中的行
我有一个包含用户和IP列表的文件,标签分开。在这个文件中,我想用用字符串替换命令结果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其
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$