1

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 of the same type on a txt. I tried this:

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

The problem is that it returns to me genes with a dash - like this one similar to NT5C1B:

NT5C1B-RDH14

However, if I search the gene individually:

grep "^NT5C1B$" list.txt

I'll not obtain the gene with the dash but only:

NT5C1B

Is there any way to do this using grep -w or other command?

Inian
  • 80,270
  • 14
  • 142
  • 161
Swimming bird
  • 57
  • 1
  • 10
  • Replace `-w` with `-x`? – Cyrus Oct 16 '20 at 12:39
  • 2
    Does this answer your question? [How to make grep only match if the entire line matches?](https://stackoverflow.com/a/4709925/3832970) – Wiktor Stribiżew Oct 16 '20 at 12:45
  • we need to see some text in the `list.txt` also, what is in your `$gene` variable? a single entry or all entries as you pasted in your first code block? – Kent Oct 16 '20 at 12:46
  • @WiktorStribiżew I think the question was closed probably too soon... "I'll not obtain the gene with the dash but only:...." I **guess** OP wants to get `Pattern-something` in the result. Also, we didn't see the input text, we don't know if it's `-x` case. Anyway, OP didn't describe the requirement clearly. – Kent Oct 16 '20 at 12:59

1 Answers1

1

Use -x as it only selects exact matches. -w can match a string if it has non-word constituent characters after the substring. See grep(1) .

jcnoe
  • 85
  • 9