简体   繁体   中英

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

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

一个简单的方法是说:

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

我相信你想要这样的东西:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM