Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • You could also use the -F option since we're not using regex patterns, just plain string matching Commented Feb 14, 2019 at 13:54
  • @glennjackman Maybe I've read this earlier, but I can't seem to find it now. Running with -F (surprisingly to me) appears to take a similar amount of time or even slightly slower (~5–10%). Hence, I'm not sure what the advantage would be. Commented Feb 14, 2019 at 21:53
  • 2
    It's possible that the RegEx engine is used so often and so widely used that they have implemented a very efficient version of it, but that a "plain search" probably has not been upgraded for 30 years. Commented Feb 15, 2019 at 3:31
  • @Sparhawk: grep presumably has a special case for regexes with no metacharacters, because that's a common use-case. It's surprising that fgrep would be slower, but it's not surprising that the overhead of noticing this special case while compiling a short pattern is negligible vs. the time to scan a large file. (If it requires a special case at all to go that fast, vs. a pattern with a character class or x.*y.) Commented Feb 15, 2019 at 14:54
  • But that's maybe an oversimplification because the input is actually many short lines (not one giant string). I forget if grep recognizes any character other than \n newline as a line separator. If not, the implicit ^ and $ can still turn into a fixed-string search like strstr(big_buf, "\n0\n"). (Or 0\n at the start of a buffer.) But we're not just looking for the first match potentially far into a big buffer, we want to efficiently filter. But anyway, in theory yes it's just a 2-byte memcmp at the start of each line, and you'd hope that both fgrep and grep would see that. Commented Feb 15, 2019 at 15:04