如何替换文本文件中的 ${} 占位符? [英] How to replace ${} placeholders in a text file?

查看:35
本文介绍了如何替换文本文件中的 ${} 占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将模板"文件的输出通过管道传输到 MySQL,该文件中散布着诸如 ${dbName} 之类的变量.替换这些实例并将输出转储到标准输出的命令行实用程序是什么?

解决方案

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.

这篇关于如何替换文本文件中的 ${} 占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆