我基本上喜欢结合的力量
grep -f
Run Code Online (Sandbox Code Playgroud)
同
awk '{ if($2=="this is where I'd like to input a file of fixed string patterns") print $0}'
Run Code Online (Sandbox Code Playgroud)
也就是说,我想用模式的输入文件(文件2)搜索文件的特定列(文件1).如果找到匹配只是:
> outputfile.txt
Run Code Online (Sandbox Code Playgroud)
从上一篇文章中,这个awk系列非常接近:
awk 'NR==FNR{a[$0]=1;next} {n=0;for(i in a){if($0~i){n=1}}} n' file1 file2
Run Code Online (Sandbox Code Playgroud)
从使用ack或awk获取一个文件中的模式或使用比grep更好的方式获取模式?
但它不会搜索文件1的特定列.我也可以使用其他工具.
您找到的示例确实非常接近您想要的,唯一的区别是您不想匹配整行($0)。
将其修改为如下所示:
awk 'NR==FNR { pats[$0]=1; next } { for(p in pats) if($2 ~ p) { print $0; break } }' patterns file
Run Code Online (Sandbox Code Playgroud)
如果您只需要固定的字符串匹配,请使用该index()函数,即替换$2 ~ p为index($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
Run Code Online (Sandbox Code Playgroud)
您可以使用==运算符来完成此操作:
awk -v col=$col 'NR==FNR { pats[$0]=1; next } { for(p in pats) if($col == p) { print $0; break } }' patterns file
Run Code Online (Sandbox Code Playgroud)