2

There is an option in grep to search the exact string "-F"

-F, --fixed-strings

Its working perfectly fine if there are two words where one is the word to be searched and another is appended with alphaumeric character. However, if the other word contains some special character like '-' then grep -F or grep -w is not matching correct result.

grep -w "hello" test1
hello
hello-

cat test1 
hello
hello-
hellotest

ideally only first line should have come in result.

Learner
  • 1,544
  • 8
  • 29
  • 55

1 Answers1

2

- is not a word character so -w will still match hello as a complete word.

You can use -x option for exact match:

grep -Fx 'hello' test1

hello
anubhava
  • 761,203
  • 64
  • 569
  • 643