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? – Omnifarious Jun 28 '17 at 23:07

Using anchors, absolutely:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.