我有文本文件,其中包含${key}
形式的哈希表中的某些键。我需要解析文本文件并用其值替换${key}
。下面是我使用
#$config is the hashtable
$document = Get-Content sample.txt
foreach ($conf in $config.Keys) {
[string]$match = '${' + $conf + '}'
$document = $document.Replace($match, $($config.$conf))
}
}
我曾尝试下面的替代品$match
串
[string]$match = "`${" + $conf + "}"
[string]$match='${'
$match+=$conf
$match+='}'
我甚至尝试硬编码字符串作为$match='${BACKUP_FOLDER}'
的代码,但在所有的情况下,它确实没有。我也尝试过-match
来查看是否匹配,但总是返回false。 我已确认文本文件包含${key}
格式的几种模式,如果我复制$match
的输出并在文件中搜索,我可以找到模式。你能指出什么是错的。我使用Powershell V5.0
样品输入
[email protected]{"BACKUP_DIR"="_Backup";"STAGING_DIR"="_Staging"}
sample.txt的内容
Overwrites the contents of new installation with old installation
Always use/as path seperator-->
<!-- <rename path="" newName=""/> -->
<!--
${BACKUP_DIR}
rename-> rename file/directory in new installation
输出:${BACKUP_DIR}
应该_Backup
请编辑问题并添加样本输入和所需的输出数据。 – vonPryz