How to print file names in a search despite processing the result and grep

I have a directory with files for testing, say file A , B and C

To keep things simple, suppose I have a command that I want to issue on each of these files and find one that gives me the correct output.

I will need a pipe myCommand fileName | grep ExpectedResult myCommand fileName | grep ExpectedResult (in my real case, I was looking for a character in the library, so it was readelf -s | grep MySymbol ).

I want to release this command from the result of the find .

I find my result with

 find . -name *.so -print0 | xargs -0 myCommand | grep ExpectedResult 

This works fine, prints ExpectedResult .

What I want to get (if I'm looking for file B ):

 A B ExpectedResult C 

Thus, I could see in which file the result was found. If I were near the grep contents of the file, I would need the -print switch in find . Unfortunately, if I need commands with channels, this will not be done.

Obviously grep -H won't execute either, because it just says (standard input) .

How can I override "outgrepping" file names? To print on stderr somehow?

I understand that I can save the file name in a variable, process it, etc., but I would like to see a simpler approach.

+7
linux bash
source share
2 answers

One easy way is to say:

 find . -type f -name "*.so" -exec sh -c "echo {} && readelf -s {} | grep mysymbol" \; 
+8
source share

I believe you need something like this:

 find . -name *.so -print0 | xargs -0 -I % sh -c 'echo % ; myCommand "%" | grep ExpectedResult' 
+3
source share

All Articles