我有文本文件,其中包含${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