[英]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.