【问题标题】:How to replace string by other file content in Makefile如何用Makefile中的其他文件内容替换字符串
【发布时间】:2017-02-06 12:23:13
【问题描述】:

我有一个包含一些 include 行的文件,我想读取文件路径并将该行替换为文件内容。

例如将<!-- include(path/filename.txt) --> 行替换为path/filename.txt 的内容

# meta info
- a
- b

# included files
<!-- include(path/filename.txt) -->

我的 Makefile:

expand-includes:
    @echo "Expand all included file"
    perl -pi -e "s/<!--\s?include\((.+)\)\s?-->/`cat $$1`/" input.txt

捕获组看起来为空$$1,脚本挂起。 我错过了什么吗?

更新: 删除 $(shell)

【问题讨论】:

  • 首先,配方无论如何都是由shell运行的,那么为什么需要$(shell ...)?其次,$1 是空的,因为 shell 会扩展它,因为它位于双引号字符串中(替换为空的第一个位置参数的值)。另外,如果我记得有关 Perl 的任何信息(我不一定记得),您想使用 \1 来指代“匹配寄存器 #1”——cat $$1 将替换名为的文件的内容$1(重复一遍,是一个空字符串),不知道是什么意思。
  • 为什么你使用 cat $$1 而在 Perl 中使用 -i 选项就地修改?
  • 感谢@MichaelLivshin 和@k-five。我想用相应的文件内容替换字符串,所以需要-i 选项。我尝试了一段时间,发现 e 修饰符正是我所需要的。 perl -pi -e 's/&lt;!--\s?include\((.+\.md)\)\s?--&gt;/`cat $$1`/e' input.txt

标签: regex shell makefile


【解决方案1】:

以下语法效果很好。

perl -p -e 's/<!--\s?include\((.+)\)\s?-->/`cat $$1`/e' input.txt > output.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2011-07-11
    • 2019-12-07
    • 1970-01-01
    相关资源
    最近更新 更多