【发布时间】:2011-03-25 02:40:15
【问题描述】:
我正在编写一个用于自定义配置文件的脚本。我想替换此文件中的多个字符串实例,并尝试使用 PowerShell 来完成这项工作。
单次替换效果很好,但是多次替换很慢,因为每次都要重新解析整个文件,而且这个文件非常大。脚本如下所示:
$original_file = 'path\filename.abc'
$destination_file = 'path\filename.abc.new'
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'something1', 'something1new'
} | Set-Content $destination_file
我想要这样的东西,但我不知道怎么写:
$original_file = 'path\filename.abc'
$destination_file = 'path\filename.abc.new'
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'something1', 'something1aa'
$_ -replace 'something2', 'something2bb'
$_ -replace 'something3', 'something3cc'
$_ -replace 'something4', 'something4dd'
$_ -replace 'something5', 'something5dsf'
$_ -replace 'something6', 'something6dfsfds'
} | Set-Content $destination_file
【问题讨论】:
标签: powershell replace