I have a program called foo
which I want to exec on each result that is found via find. So something like this:
find . -name '*.o' -type f -exec foo {} \;
I want to grep the output of all of these invocations of foo
for a certain string bar. So I add this:
find . -name '*.o' -type f -exec foo {} \; | grep bar
But I lose the original information about what files the matches are coming from. I tried adding -fprintf /dev/stderr '%p\n'
to the find
command but now it looks like stdout is gone, because no grep results are printed.
How can I get each filename printed to the output and THEN have the grep results corresponding to that file printed after?
Alternatively, if there were some way to make the -H
argument of grep
work that would be fine too, but as written it won't work because I'm just passing text from stdin, and grep
doesn't know the filename. I tried various incantations of xargs
, but I couldn't get any of that to work either.
grep
support a--label
option?