Why do my results appear to differ between ag and grep?

165 views Asked by At

I'm having trouble correctly (and safely) executing the right regex searches with grep. I seem to be able to do what I want using ag

What I want to do in plain english:

Search my current directory (recursively?) for files that have lines containing both the words "nested" and "merge"

Successful attempt with ag:

$ ag --depth=2 -l "nested.*merge|merge.*nested" .
   scratch.md
   scratch.rb

Unsuccessful attempt with grep:

$ grep -elr 'nested.*merge|merge.*nested' .
   grep: nested.*merge|merge.*nested: No such file or directory
   grep: .: Is a directory

What am I missing? Also, could either approach be improved?

Thanks!

2

There are 2 answers

0
Xiangyu.Wu On BEST ANSWER

You can use grep -lr 'nested.*merge\|merge.*nested' or grep -Elr 'nested.*merge|merge.*nested' for your case.
Besides, for the latter one, E mean using ERE regular expression syntax, since grep will use BRE by default, where | will match character | and \| mean or.
For more detail about ERE and BRE, you can read this article

0
Mort On

You probably want -E not -e, or just egrep.

A man grep will make you understand why -e gave you that error.