从文件中获取的模式,比较到另一个文件的一列,打印匹配行,用awk [英] Obtain patterns from a file, compare to a column of another file, print matching lines, using awk

查看:118
本文介绍了从文件中获取的模式,比较到另一个文件的一列,打印匹配行,用awk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想的力量结合起来。

I'd essentially like to combine the power of

grep -f 

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

这就是说,我想与模式输入文件(文件2)搜索文件(文件1)的特定列。如果简单地找到匹配的:

Which is to say, I'd like to search a specific column of a file (File 1) with an input file of patterns (File 2). If a match is found simply:

> outputfile.txt

从previous后,这AWK行是非常接近:

From a previous post, this awk line is really close:

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

从<一个拍摄href=\"http://stackoverflow.com/questions/9936962/obtain-patterns-in-one-file-from-another-using-ack-or-awk-or-better-way-than-gre\">Obtain使用ACK或AWK或更好的方式比grep的一个文件中的模式从另一个?

但它不搜索文件1的特定列我接受其他工具为好。

But it doesn't search a specific column of file 1. I'm open to other tools as well.

推荐答案

您找到的例子确实是非常接近你想要的东西,唯一的区别是,你不希望整条线路匹配( $ 1,0 )。

The example you found is indeed very close to what you want, the only difference is that you don't want to match the whole line ($0).

修改它是这样的:

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

如果您只需要一个固定的字符串匹配,使用指数()函数来代替,如更换 $ 2〜P 指数($ 2,p)

If you only need a fixed string match, use the index() function instead, i.e. replace $2 ~ p with index($2, p).

您也可以提供列号作为参数传递给AWK,例如:

You could also provide the column number as an argument to awk, e.g.:

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

这篇关于从文件中获取的模式,比较到另一个文件的一列,打印匹配行,用awk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆