Can a grep command be constructed to so it will return all lines with only a specific word or phrase but won't return any lines that contain the same word or phrase when it contains additional words? For example, if a list contains:

Test Num 3
Test Nbr4 
Test Random text 
Test with more Random Test data.
What if a Test contains this text?

I would like to find a way to return only the lines that contains the single word "Test". In this case, it would return only line 1.

  • What about whitespace before and after the word or lines that don't contain the word at all? Commented Jun 28, 2017 at 23:07

1 Answer 1


Using anchors, absolutely:

$ grep -e '^Test$' <file>
  • 1
    Or grep -x Test file
    – Jeff Schaller
    Commented Jun 28, 2017 at 19:07
  • The OP probably also does not think whitespace is important. Commented Jun 28, 2017 at 23:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .