[英]Unexpected perl regex substitution output in makefile using fgrep
受prwhite在GitHub上的此规则启发
help:
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'`
我正在尝试制作一个用空格替换目标文件的版本。 我想用所有空格替换字符串的一部分。 使用文件input.txt
:
spec: $(ALL_FILES) ## Runs all specs
myst-spec: $(ALL_FILES) ## Runs just the in-language specs
build: $(SOURCE_FILES) ## Builds myst into an executable
check: $(ALL_FILES) ## Runs all crystal specs
clean: ## Cleans (deletes) docs and executables
help: ## Show this help.
这是预期的输出:
spec: ## Runs all specs
myst-spec: ## Runs just the in-language specs
build: ## Builds myst into an executable
check: ## Runs all crystal specs
clean: ## Cleans (deletes) docs and executables
help: ## Show this help.
这是我目前拥有的规则:
help:
@fgrep -h "##" input.txt | fgrep -v fgrep | \
perl -pe 's/(.*):(.*)##(.*)/("$1:" . (" " x length($2)) . " $3")/e'
注意这里使用了fgrep
,因为实际上这应该在makefile本身上运行,但是我在这里使用input.txt
,因此您不必处理整个makefile。
出乎意料的是,我得到以下输出:
:
:
:
:
:
:
最让我困惑的是,如果我直接在input.txt
上运行perl脚本(例如: cat input.txt | perl -pe 's/(.*):(.*)##(.*)/("$1:" . (" " x length($2)) . " $3")/e'
)–我得到了预期的输出。 它提供不同输出的原因是什么? 我意识到它(可能)与fgrep有关,但我不知道它会产生什么变化,所以我在这里。
如果要在配方中使用$
,则必须将其加倍至$$
以使其摆脱make的作用。
help:
@fgrep -h "##" input.txt | fgrep -v fgrep | \
perl -pe 's/(.*):(.*)##(.*)/("$$1:" . (" " x length($$2)) . " $$3")/e'
上面可以简化:
help:
@perl -ne'next if !/##/; s/(.*):(.*)##(.*)/ "$$1:".( " " x length($$2) )." $$3" /e; print'
或(5.10+):
help:
@perl -ne'next if !/##/; s/:\K(.*)(?=##)/ " " x length($$1) /e; print'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.