简体   繁体   English

遍历makefile中不包含某些文本的文件

[英]Loop through files which do not contain certain texts in makefile

I want to go through a set of files in makefile : 我想浏览一下makefile一组文件:

alltest:    all
            for f in $(FILES); do \
                echo $$f; \
            done

I want to write $(FILES) such that it regards the files of /abc/*.txt which do not contain ERROR1 or ERROR2 in their body. 我想编写$(FILES)这样它/abc/*.txt文件ERROR2其主体中不包含ERROR1ERROR2的文件。

Too look for a set of files which contain ERROR1 or ERROR2 in their body, I use find -iname '/abc/*.txt' | xargs grep -e "ERROR1" -e "ERROR2" 太寻找一组文件中包含ERROR1ERROR2的文件,我使用find -iname '/abc/*.txt' | xargs grep -e "ERROR1" -e "ERROR2" find -iname '/abc/*.txt' | xargs grep -e "ERROR1" -e "ERROR2"

Could anyone tell me how to do the complement of the set, also how to integrate the shell command into the makefile? 谁能告诉我如何做集合的补集,以及如何将shell命令集成到makefile中?

First things first…does this do what you think and want? 第一件事……这是您的想法和想要的吗?

find -iname '/abc/*.txt'

It doesn't find files in a subdirectory for me (on Mac OS X 10.8.5). 在我的子目录中找不到文件(在Mac OS X 10.8.5上)。 Where find . -iname '*.txt' 在哪里find . -iname '*.txt' find . -iname '*.txt' finds some files, find . -iname '/path/*.txt find . -iname '*.txt'查找一些文件, find . -iname '/path/*.txt find . -iname '/path/*.txt produces no output, and neither does -ipath . find . -iname '/path/*.txt -ipath . find . -iname '/path/*.txt产生输出, find . -iname '/path/*.txt产生。 However, find . -ipath '*/path/*.txt' 但是, find . -ipath '*/path/*.txt' find . -ipath '*/path/*.txt' does produce a list of files. find . -ipath '*/path/*.txt'会产生文件列表。 So, for the time being, we'll correct your find command to: 因此,暂时,我们将把您的find命令更正为:

find . -ipath '*/abc/*.txt'

Next, you run: 接下来,您运行:

xargs grep -e "ERROR1" -e "ERROR2"

This will produce lines with both the names of the files and the message. 这将产生包含文件名和消息的行。 If you wanted the names of the files which include a match, you need to add -l to the command: 如果想要包含匹配项的文件名,则需要在命令中添加-l

find . -ipath '*/abc/*.txt' |
xargs grep -l -e "ERROR1" -e "ERROR2"

However, you want to list only the files that do not match. 但是,您只想列出不匹配的文件。 For that, if you have GNU grep , you can use the -L option: 为此,如果您具有GNU grep ,则可以使用-L选项:

find . -ipath '*/abc/*.txt' |
xargs grep -L -e "ERROR1" -e "ERROR2"

If you don't have GNU grep , then you have to work a lot harder: 如果您没有GNU grep ,那么您必须更加努力地工作:

find . -ipath '*/abc/*.txt' |
tee file.names |
xargs grep -l -e "ERROR1" -e "ERROR2" > matching.file.names
comm -23 file.names matching.file.names

The tee command captures a copy of the list of file names in the file file.names (such originality). tee命令捕获文件file.names中文件名列表的副本(这种独创性)。 The grep command captures the list of matching file names in matching.file.names . grep命令捕获文件名匹配的列表matching.file.names The names will be in the same order in both files. 两个文件中的名称将以相同的顺序排列。 The ones that appear in file.names but not in matching.file.names are the ones you want. 出现在那些file.names但不是在matching.file.names是你想要的人。 By default, the comm file.names matching.file.names command would print 3 columns: those lines found only in the first file, those found only in the second file, and those found in both files. 默认情况下, comm file.names matching.file.names命令将打印3列:仅在第一个文件中找到的行,仅在第二个文件中找到的行以及在两个文件中都找到的行。 By suppressing the output of columns 2 and 3 with the -23 option, we get only the names in the first file that are not found in the other — which is the list you want. 通过使用-23选项抑制第2列和第3列的输出,我们仅获得第一个文件中另一个文件中找不到的名称-这就是您想要的列表。

Where you take the list from there is up to you… 您可以从哪里获取清单,取决于您……

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

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