Grep search returning a different line

grepregular expression

This is my grep search

 grep 'Invoker_Slark*' true_pairscore.txt

But it returns the line Invoker_Slardar. Even though the file contains Invoker_Slark. Why is that?

Best Answer

The reason is that Invoker_Slark* is considered a regular expression where k* means: "zero or more occurrences of k"

That's different from shell globbing patterns where * means 0 or more characters.

To search for Invoker_Slark anywhere in the line, you need:

  1. grep 'Invoker_Slark' true_pairscore.txt or

  2. grep -x '.*Invoker_Slark.*' true_pairscore.txt

If the search string must be at the beginning of the line then this has to be changed to:

  1. grep '^Invoker_Slark' true_pairscore.txt or

  2. grep -x 'Invoker_Slark.*' true_pairscore.txt

Related Question