I'm searching for files based on a regular expression, and then I'm trying to search those files for content. So, for example, I have something like
#Find all C++ files that match a certain pattern and then search them
find . -name "*.cpp" | grep "<name regex>" | xargs grep "<content regex>"
The problem I'm running into is that some of the paths have spaces in them, which confuses xargs
. I know that if I was just using find
, I could use the -print0
argument (along with the -0
argument on xargs
) to keep xargs from treating spaces as delimiters. Is there something similar with grep
?
Or am I approaching this problem in the wrong way entirely? Naively, find
to grep
to xargs grep
makes sense to me, but I'm open to other approaches that yield the same results.
xargs
by using-i
parameter, a lacat sample.txt | grep "pat t ern" | xargs -i grep "{}"
-- the curly braces tell it where to position the argument. The manual tells me that-i
is deprecated in favor of-I
so maybe worth taking a look at that too.