简体   繁体   English

尽管处理结果和grep,如何在find中打印文件名

[英]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 . 我有一个目录,包含要测试的文件,比如文件ABC

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 myCommand fileName | grep ExpectedResult (in my real case I was looking for a symbol in a library, so it was readelf -s | grep MySymbol ). myCommand fileName | grep ExpectedResult (在我的实际情况中,我在库中寻找符号,所以它是readelf -s | grep MySymbol )。

I want to issue this command on a result of find command. 我想在find命令的结果上发出这个命令。

I find my result with 我找到了我的结果

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

This works ok, prints ExpectedResult . 这样可以,打印ExpectedResult

What I want to receive is (assuming that the file I look for is B ): 我想收到的是(假设我找的文件是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 . 如果我只是要grep文件的内容,我需要在find使用-print开关。 Unfortunately, if I need piped commands, this would not do. 不幸的是,如果我需要管道命令,这是行不通的。

Obviously, grep -H wouldn't do either, because it will just say (standard input) . 显然, grep -H也不会这样做,因为它只会说(standard input)

How can I override "outgrepping" the file names? 如何覆盖“outgrepping”文件名? Print it on stderr somehow? 不知何故在stderr上打印?

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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