单遍填充文件中的占位符

发布于 2024-07-11 07:19:59 字数 544 浏览 7 评论 0原文

我有一个带有占位符字符串的骨架文本文件:

blah blah blah
blah $PLACEHOLDER_1$
blah
$PLACEHOLDER_2$

等等。 占位符的具体“形式”并不重要——我可以将它们更改为最适合具体实现的任何形式。

我有一个 bash 脚本,其中我知道占位符的值,并且我需要生成一个新文件,并将占位符替换为值。

#! /bin/sh
PLACEHOLDER_1 = 'string 1'
PLACEHOLDER_2 = 'multiline 
string 
2'
# TODO: Generate file output.txt from file output.template 
#       using placeholders above.

我可以使用 sed 多次执行此操作,但这并不有趣。 我不想想使用Perl。 我只想使用 textutils 和 bash 本身。

一次完成我想要的事情的最佳方法是什么?

I have a skeleton text file with placeholder strings:

blah blah blah
blah $PLACEHOLDER_1$
blah
$PLACEHOLDER_2$

and so on. Specific "form" of placeholders does not matter -- I may change them to whatever most comfortable for specific implementation.

I have a bash script where I know values for placeholders, and I need to generate a new file, with placeholders replaced with values.

#! /bin/sh
PLACEHOLDER_1 = 'string 1'
PLACEHOLDER_2 = 'multiline 
string 
2'
# TODO: Generate file output.txt from file output.template 
#       using placeholders above.

I may do this in multiple passes with sed, but it is not fun. I do not want to use Perl. I want to use textutils and bash itself only.

What is the best way to do what I want in a single pass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光礼记 2024-07-18 07:20:00

这是一种不使用 sed 的方法:

首先,稍微修改一下模板文件,其中占位符是 bash 变量:

blah blah blah
blah $PLACEHOLDER_1
blah
$PLACEHOLDER_2

和脚本:

#! /bin/sh
templatefile=output.template
outputfile=output.txt

PLACEHOLDER_1='string 1'

PLACEHOLDER_2='multiline 
string 
2'

# DONE: Generate file output.txt from file output.template 
#       using placeholders above.

echo "$(eval "echo \"$(cat $templatefile)\"")" > $outputfile

这是一个版本,演示了脚本中包含的模板,但有所不同。 它还演示了默认值,这些值也可以在模板文件版本中使用,此外您还可以在模板中进行数学计算:

#! /bin/sh
template='blah blah blah
blah $PLACEHOLDER_1
blah
${PLACEHOLDER_2:-"some text"} blah ${PLACEHOLDER_3:-"some
lines
of
text"} and the total is: $((${VAL_1:-0} + ${VAL_2:-0}))'
# default operands to zero (or 1) to prevent errors due to unset variables
outputfile=output.txt

# gears spin, bells ding, values for placeholders are computed

PLACEHOLDER_1='string 1'

PLACEHOLDER_2='multiline 
string 
2'

VAL_1=2

VAL_2=4

unset PLACEHOLDER_3 # so we can trigger one of the defaults

# Generate file output.txt from variable $template 
#       using placeholders above.

echo "$(eval "echo \"$template\"")" > $outputfile

没有 sed,没有循环,只有毛茸茸的嵌套和引号。 我很确定所有引用都会保护您免受模板文件中恶意内容的影响,但我不保证这一点。

Here's a way to do it without sed:

First, a slightly modified template file in which the placeholders are bash variables:

blah blah blah
blah $PLACEHOLDER_1
blah
$PLACEHOLDER_2

And the script:

#! /bin/sh
templatefile=output.template
outputfile=output.txt

PLACEHOLDER_1='string 1'

PLACEHOLDER_2='multiline 
string 
2'

# DONE: Generate file output.txt from file output.template 
#       using placeholders above.

echo "$(eval "echo \"$(cat $templatefile)\"")" > $outputfile

Here's a version that demonstrates a template contained within the script, but with a twist. It also demonstrates default values, which can also be used in the template file version, plus you can do math in the template:

#! /bin/sh
template='blah blah blah
blah $PLACEHOLDER_1
blah
${PLACEHOLDER_2:-"some text"} blah ${PLACEHOLDER_3:-"some
lines
of
text"} and the total is: $((${VAL_1:-0} + ${VAL_2:-0}))'
# default operands to zero (or 1) to prevent errors due to unset variables
outputfile=output.txt

# gears spin, bells ding, values for placeholders are computed

PLACEHOLDER_1='string 1'

PLACEHOLDER_2='multiline 
string 
2'

VAL_1=2

VAL_2=4

unset PLACEHOLDER_3 # so we can trigger one of the defaults

# Generate file output.txt from variable $template 
#       using placeholders above.

echo "$(eval "echo \"$template\"")" > $outputfile

No sed, no loops, just hairy nesting and quotes. I'm pretty sure all the quoting will protect you from malicious stuff in a template file, but I'm not going to guarantee it.

没︽人懂的悲伤 2024-07-18 07:20:00

您仍然可以使用 sed 一次性完成替换。 您只需在一个命令中指定所有替换即可。

例如。

sed -i 's/PLACEHOLDER_1/string 1/g;s/PLACEHOLDER_2/string 2/g' <file>

You can still use sed to do the replace in a single pass. You just need to specify all the replacements in one command.

eg.

sed -i 's/PLACEHOLDER_1/string 1/g;s/PLACEHOLDER_2/string 2/g' <file>
慢慢从新开始 2024-07-18 07:20:00

在前面的答案的基础上,也许使用数组并计算 sed 字符串?

#!/bin/sh
PLACEHOLDER[0]='string 1'
PLACEHOLDER[1]='multiline 
string 
2'

s="sed -i "
for(( i=0 ; i<${#PLACEHOLDER[*]} ; i++ )) ; do 
    echo ${PLACEHOLDER[$i]}
    s=$s"s/PLACEHOLDER_$i/${PLACEHOLDER[$i]}/g;"
done
echo $s

不过,似乎在多行字符串上失败了。

我不知道 Bash 数组的可移植性如何。 上面的代码片段使用“GNU bash,版本 3.2.17(1)-release (i386-apple-darwin9.0)”进行了测试

Building on the previous answer, perhaps use an array and compute the sed string?

#!/bin/sh
PLACEHOLDER[0]='string 1'
PLACEHOLDER[1]='multiline 
string 
2'

s="sed -i "
for(( i=0 ; i<${#PLACEHOLDER[*]} ; i++ )) ; do 
    echo ${PLACEHOLDER[$i]}
    s=$s"s/PLACEHOLDER_$i/${PLACEHOLDER[$i]}/g;"
done
echo $s

Seems to fail on the multi-line strings, though.

I don't know how portable Bash arrays might be. Above snippet tested with "GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)"

感受沵的脚步 2024-07-18 07:20:00

我的 bash 唯一解决方案:

TEMPLATE='
foo
$var1
bar
$var2'
eval "echo \"$TEMPLATE\""

My bash only solution:

TEMPLATE='
foo
$var1
bar
$var2'
eval "echo \"$TEMPLATE\""
妳是的陽光 2024-07-18 07:20:00

我只是偶然发现了这个问题,因为我只是在寻找完全相同的问题,并且我找到了 envsubst(1)

如果您不介意使用环境变量,则可以使用envsubst

PLACEHOLDER_1='string 1' PLACEHOLDER_2='multiline 
string 
2' envsubst < output.template 

如果您有很多变量,您可以将它们存储在一个文件中,然后source它(记住使用export 在源文件的末尾!)

I just stumbled upon this question because I was just looking for the exact same, and I found envsubst(1).

You can use envsubst if you don't mind using environment variables:

PLACEHOLDER_1='string 1' PLACEHOLDER_2='multiline 
string 
2' envsubst < output.template 

If you have a lot of variables you can store them in a file and just source it (remember to use export at the end of the sourced file!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文