问题描述
我有一个占位符字符串的框架文本文件:
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.
我有一个bash脚本,我知道占位符值,我需要生成一个新的文件,用值替换的占位符。
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.
我可以多遍与SED做到这一点,但它是不好玩。我做的不可以想使用Perl。我想用文本实用程序,只来砸自己。
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?
推荐答案
您仍可以使用SED做一次通过替换。你只需要在一个命令来指定所有的替代品。
You can still use sed to do the replace in a single pass. You just need to specify all the replacements in one command.
如:
sed -i 's/PLACEHOLDER_1/string 1/g;s/PLACEHOLDER_2/string 2/g' <file>
这篇关于填写占位符,在单次通过文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!