I have a directory with files to test, say file A
, B
and C
.
To make things easy, let's assume I have a command I want to issue on each of these files and find the one that gives me a proper output.
I will need a pipe myCommand fileName | grep ExpectedResult
(in my real case I was looking for a symbol in a library, so it was readelf -s | grep MySymbol
).
I want to issue this command on a result of find
command.
I find my result with
find . -name *.so -print0 | xargs -0 myCommand | grep ExpectedResult
This works ok, prints ExpectedResult
.
What I want to receive is (assuming that the file I look for is B
):
A
B
ExpectedResult
C
This way I could see in which file the result has been found. If I was just about to grep
the content of the file, I would need a -print
switch in find
. Unfortunately, if I need piped commands, this would not do.
Obviously, grep -H
wouldn't do either, because it will just say (standard input)
.
How can I override "outgrepping" the file names? Print it on stderr somehow?
I realize I could save my file name in a variable, process it etc, but I'd love to see a simpler approach.
One simple way would be to say:
find . -type f -name "*.so" -exec sh -c "echo {} && readelf -s {} | grep mysymbol" \;
I believe you want something like this:
find . -name *.so -print0 | xargs -0 -I % sh -c 'echo % ; myCommand "%" | grep ExpectedResult'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With