且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何替换文本文件中的 ${} 占位符?

更新时间:2023-02-23 14:49:54

Sed!>

给定的 template.txt:

号码是 ${i}这个词是 ${word}

我们只想说:

sed -e "s/${i}/1/" -e "s/${word}/dog/" template.txt

感谢 Jonathan Leffler 提供将多个 -e 参数传递给同一个 sed 调用的技巧.

I want to pipe the output of a "template" file into MySQL, the file having variables like ${dbName} interspersed. What is the command line utility to replace these instances and dump the output to standard output?

Sed!

Given template.txt:

The number is ${i}
The word is ${word}

we just have to say:

sed -e "s/${i}/1/" -e "s/${word}/dog/" template.txt

Thanks to Jonathan Leffler for the tip to pass multiple -e arguments to the same sed invocation.