Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Are you looking for lines which begin with Invoker_Slark or for all which contain it? – Hauke Laging Apr 17 '14 at 21:10
@HaukeLaging : Yes, I was looking for lines that begin with Invoker_Slark. So that means even Invoker_Slarkaslkjfd is a valid result. But not Invoker_Slardar. – Ashwin Apr 17 '14 at 21:40
My point is: The code in my answer would match a line foo_Invoker_Slarkaslkjfd, too. Is that intended or would that be an invalid result? – Hauke Laging Apr 17 '14 at 21:46
up vote 12 down vote accepted

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

share|improve this answer
I thought that the * is like a substitution for "any number of characters that follow". Which search can fetch me these 2 results? "Invoker_Slarkasdfd" and "Invoker_Slarkoeirute" – Ashwin Apr 17 '14 at 21:35
@Ashwin: That is for glob patterns, not regular expressions. Glob is used for shell patterns (expanding to filenames), and a few other places. Regular expressions are used in grep (the 're' in 'grep' stands for 'regular expression'), sed, and a lot of other places using search patterns. See, for example enwp.org/Glob_pattern and enwp.org/Regular_expression respectively. – Johan E Apr 17 '14 at 22:48
Putting .* at the very end of a grep pattern does nothing except possibly slowing down the matching. It would not match any lines that did not match without the .* at the end. – Johan E Apr 17 '14 at 23:48
(cont.) With a pattern of ^INITIAL, the match can be concluded after matching the first 7 chars of each line. With ^INITIAL.*, after matching the 7 first characters grep has to traverse the rest of the line before the whole pattern match can be concluded (* is ’greedy,’ matching the longest possible run of characters). This MAY be optimized away when .* appears at the end of the pattern... – Johan E Apr 17 '14 at 23:49
@JohanE That is correct but the sense of this being part of my answer is not to recommend this way but to show the analogy for shell pattern matchings * in a regex. I admit: There is a difference. The shell needs it because of its implicit $ (text$ vs. text.*$). But this info should be useful for other cases. – Hauke Laging Apr 18 '14 at 0:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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