unix – 从文件中获取模式,与另一个文件的列进行比较,使用awk打印匹配的行

我基本上喜欢结合的力量
grep -f

awk '{ if($2=="this is where I'd like to input a file of fixed string patterns") print $0}'

也就是说,我想用模式的输入文件(文件2)搜索文件的特定列(文件1).如果找到匹配只是:

> outputfile.txt

上一篇文章中,这个awk系列非常接近:

awk 'NR==FNR{a[$0]=1;next} {n=0;for(i in a){if($0~i){n=1}}} n' file1 file2

取自Obtain patterns in one file from another using ack or awk or better way than grep?

但它不会搜索文件1的特定列.我也可以使用其他工具.

你找到的例子确实非常接近你想要的,唯一的区别是你不想匹配整行($0).

将其修改为以下内容

awk 'NR==FNR { pats[$0]=1; next } { for(p in pats) if($2 ~ p) { print $0; break } }' patterns file

如果您只需要一个固定的字符串匹配,请使用index()函数,即用索引($2,p)替换$2~p.

你也可以提供列号作为awk的参数,例如:

awk -v col=$col 'NR==FNR { pats[$0]=1; next } { for(p in pats) if($col ~ p) { print $0; break } }' patterns file

编辑 – 整个字段匹配

您可以使用==运算符完成此操作:

awk -v col=$col 'NR==FNR { pats[$0]=1; next } { for(p in pats) if($col == p) { print $0; break } }' patterns file

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...