17

I've got a big text file. I need to extract all the lines which contains the exact word "DUSP1". Here an example of the lines:

9606    ENSP00000239223 DUSP1   BLAST
9606    ENSP00000239223 DUSP1-001 Ensembl

I want to retrieve the first line but not the second one.

I tried several commands as:

grep -E "^DUSP1"
grep '\<DUSP1\>'
grep '^DUSP1$'
grep -w DUSP1

But none of them seem to work. Which option should I use?

jww
  • 90,984
  • 81
  • 374
  • 818
Titus Pullo
  • 3,581
  • 12
  • 43
  • 63
  • How exactly is the "exact word" defined? And your 3rd example would only find lines with only the word "DUSP1" ... So you want lines with "^DUSP1[[:space:]]+" ? – Dominik Sandjaja Jul 12 '13 at 13:29
  • 3
    Could you provide sample file content. The 2nd, 3rd, 4th commands works for me. – falsetru Jul 12 '13 at 13:29

3 Answers3

32

If you want to grep exactly the whole word, you can use word boundaries like this:

grep '\bDUSP1\b'

This matches for the exact word at the beginning and at the end.

Phitherek_
  • 724
  • 1
  • 8
  • 13
19

The problem you are facing is that a dash (-) is considered by grep as a word delimiter.

You should try this command :

grep '\sDUSP1\s' file

to ensure that there's spaces around your word.
Or use words boundaries :

grep '\bDUSP1\b' file
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
2

adding to what sputpick said, it could either be that or:

grep '\sDUSP1$' file 

if the DUSP1 is the end of the line.

slfan
  • 8,665
  • 115
  • 63
  • 77
indradip
  • 21
  • 1