Learner
Learner

Reputation: 1634

Grep fixed string is not grepping exact string, string end with extra character '-' is also coming

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.

Upvotes: 2

Views: 1284

Answers (1)

anubhava
anubhava

Reputation: 785246

- 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

Upvotes: 2

Related Questions