Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

bash - 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:

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.

question from:https://stackoverflow.com/questions/65920395/grep-exact-match-no-dash

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need to "anchor" the search patterns at the end of the lines and so:

NOL6$
NPPC$
NPRL2$

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:

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

With sed, we are replacing the end of the line with $


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...