使用PowerShell,我想用MyValue
替换给定文件中所有确切出现的[MYID]
。最简单的方法是什么?
发布于 2013-06-17 17:35:15
使用(V3版本):
(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt
或者对于V2:
(Get-Content c:\temp\test.txt) -replace '\[MYID\]', 'MyValue' | Set-Content c:\temp\test.txt
发布于 2016-07-21 04:04:14
上面的那个只对“一个文件”运行,但是你也可以在你的文件夹中运行多个文件:
Get-ChildItem 'C:yourfile*.xml' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace '[MYID]', 'MyValue' }) |
Set-Content $_
}
发布于 2013-06-17 17:43:44
您可以尝试如下所示:
$path = "C:\testFile.txt"
$word = "searchword"
$replacement = "ReplacementText"
$text = get-content $path
$newText = $text -replace $word,$replacement
$newText > $path
https://stackoverflow.com/questions/17144355
复制相似问题