简体   繁体   English

make:使用grep在文件夹内的所有文件中查找特定的字符串

[英]make: Use grep to look for specific string in all files within a folder

I'd like to look for a specific set of strings in files with a specific file extension (here it's .textfile ) I have in the current folder. 我想在当前文件夹中具有特定文件扩展名的文件(这里是.textfile )中查找一组特定的字符串。 For this, I want to use make . 为此,我想使用make

Beforehand, I convert all .pdf files to .txt files and rename their .txt extension to .textfile . 事先,我将所有.pdf文件转换为.txt文件,并将其.txt扩展名重命名为.textfile The filename itself is to be left "unharmed" during the conversion. 在转换过程中,文件名本身将保持“不受损害”。 My code so far: 到目前为止,我的代码:

#Variables that save the filenames of all .pdf files
SOURCE=$(wildcard *.pdf)
OBJECT=$(SOURCE:.pdf=.textfile)

#The conversion target
textfile: $(OBJECT)                     
%.textfile: %.pdf
    @pdftotext -f $(FROM_PAGE) $< $@

To look for a set of string within these files, I've added this to the code: 为了在这些文件中查找一组字符串,我将其添加到了代码中:

lookfor-%: $(OBJECT)
    grep -H % $<

So calling make lookfor-Foo would look for "Foo" in all .converted files within the folder. 因此,调用make lookfor-Foo会在文件夹内所有.converted文件中查找“ Foo”。 If there is a match, it would print this to stdin: 如果存在匹配项,则将其打印到标准输入:

file1.converted: Foo is here
file231.converted: here is Foo for you!

However, my Makefile doesn't do this. 但是,我的Makefile没有执行此操作。 It always prints out the exact same 2 lines as a "match", even though the set of strings I'm looking for isn't even in any of these lines. 即使我要查找的字符串集甚至不在这些行中,它也总是打印出与“ match”完全相同的两行。

What am I doing wrong? 我究竟做错了什么?

Your rule is incorrect. 您的规则不正确。 You need to use the pattern stem $* as the search pattern for grep ... 您需要使用模式词干$*作为grep的搜索模式...

lookfor-%: $(OBJECT)
    grep -H '$*' $<

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

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