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!
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