如何从字符串列表的文件用grep精确匹配 [英] How to grep exact matches from a file of a list of strings

查看:387
本文介绍了如何从字符串列表的文件用grep精确匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件与字符串像这样的列表一栏:

I have a file A with one column with a list of strings like this:

ADAMTS9
AIP
....

我想用在文件中的字符串到grep包含这些文件B和文件B线是这样的:

I want to use the strings in file A to grep the lines that contains them in file B and file B looks like this:

chr13   50571142        50592603        ADAMTS9  21461   +
chr19   50180408        50191707        AIP   11299   +
chr19   50180408        50193000        AIP-S1   6532    -

我用:

grep -F -i -w -f A B 

和它能够使用grep上面的所有3条线。不过,我只希望前两行被grep'ed和AIP-S1的第三行是不是与AIP完全匹配。

and it was able to grep all the 3 lines above. However, I only want the first two lines to be grep'ed and the third line with AIP-S1 isn't an exact match with AIP..

谁能告诉我如何修复命令这样做呢?

Can someone tell me how to fix the command to do that?

感谢。

推荐答案

您可以用awk来代替:

You can use awk instead:

awk 'FNR==NR{a[$1];next} ($4 in a)' A B
chr13   50571142        50592603        ADAMTS9  21461   +
chr19   50180408        50191707        AIP   11299   +

或在任何领域内搜索:

awk 'FNR==NR{a[$1];next} {for (i=1; i<=NF; i++) if ($i in a) print}' A B

这篇关于如何从字符串列表的文件用grep精确匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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