我正在编写一个定制配置文件的脚本。我想在该文件中替换多个字符串实例,并尝试使用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
$c = Get-Content $original_file; $c | ... | Set-Content $original_file
的操作。 - dahlbyk