Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search make:使用grep在文件夹中的所有文件中查找特定字符串_Search_Text_Makefile_Grep - Fatal编程技术网

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

Search make:使用grep在文件夹中的所有文件中查找特定字符串,search,text,makefile,grep,Search,Text,Makefile,Grep,我想在当前文件夹中具有特定文件扩展名的文件(这里是.textfile)中查找特定字符串集。为此,我想使用make 在此之前,我将所有.pdf文件转换为.txt文件,并将其.txt扩展名重命名为.textfile。在转换过程中,文件名本身将保持“未受损”。到目前为止,我的代码是: #Variables that save the filenames of all .pdf files SOURCE=$(wildcard *.pdf) OBJECT=$(SOURCE:.pdf=.textfile)

我想在当前文件夹中具有特定文件扩展名的文件(这里是
.textfile
)中查找特定字符串集。为此,我想使用make

在此之前,我将所有
.pdf
文件转换为
.txt
文件,并将其
.txt
扩展名重命名为
.textfile
。在转换过程中,文件名本身将保持“未受损”。到目前为止,我的代码是:

#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) $< $@
但是,我的Makefile不这样做。它总是打印出与“匹配”完全相同的两行,即使我要查找的字符串集甚至不在这些行中


我做错了什么?你的规则不正确。您需要使用模式stem
$*
作为
grep
的搜索模式

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

Wow!成功了,但为什么?“$*”到底代表什么?为什么要引用它?
%
仅在模式规则的目标/先决条件中是特殊的。这只是食谱中的一个简单角色。在配方中,您必须使用自动变量:
$*
自动变量扩展到“stem”(与目标中的
%
匹配的部分),这是您想要的。引用变量是因为您正在将其值传递给shell,并且不希望shell解释特殊字符(请考虑是否运行了
make'foo.*bar'
)。从
$*
中可以看出,“隐式规则匹配的词干”(请参见模式如何匹配)。如果目标是dir/a.foo.b,目标模式是a.%.b,则茎是dir/foo”。单引号只是为了防止模式包含任何可能被shell解释的特殊字符——可能只是我的偏执:——)
file1.converted: Foo is here
file231.converted: here is Foo for you!
lookfor-%: $(OBJECT)
    grep -H '$*' $<