简体   繁体   English

Grep 完全匹配无破折号

[英]Grep exact match no dash

I have a list of genes like this one saved on a variable:我有一个这样的基因列表,保存在一个变量中:

NOL6
NPPC
NPRL2
NRG1
NT5C1B
NUDT19
OSER1
PAEP
PARD3
PCDHA4

I want to grep all these genes at another list:我想将 grep 所有这些基因放在另一个列表中:

chr3 NPRL2
chr5 NT5C1B
chr5 NT5C1B-RDH14
chr2 NUDT19
chr21 ABC

I tried this:我试过这个:

grep -w "^$genes$" list.txt

The problem is that it returns to me genes with a dash too:问题是它也用破折号返回给我的基因:

    chr3 NPRL2
    chr5 NT5C1B
    chr5 NT5C1B-RDH14
    chr2 NUDT19

I tried to use -x but it doesn't work as the complete line doesn't match because of the first column.我尝试使用-x但它不起作用,因为第一列导致整行不匹配。

You will need to "anchor" the search patterns at the end of the lines and so:您需要在行尾“锚定”搜索模式,因此:

NOL6$
NPPC$
NPRL2$

etc.... ETC....

If you cannot amend the original file with the list of patterns, you can use sed and so with the patterns in file1 and the file to search as file:如果您无法使用模式列表修改原始文件,您可以使用 sed 等与 file1 中的模式和要搜索的文件作为文件:

grep -v -f <(sed 's/$/$/' file1) file

With sed, we are replacing the end of the line with $使用 sed,我们将行尾替换为 $

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM