powershell - 如何在Powershell的一条语句中使用-replace两次

标签 powershell

我试图两次在Powershell中使用-replace函数,但是我能做到这一点的唯一方法是使用以下代码:

$data = Get-Content c:\test.txt
$data -replace 'A','apple' | Out-File c:\newtest.txt
$data = Get-Content c:\newtest.txt
$data -replace 'B','blue' | Out-File c:\newtest2.txt

我相信应该有一种方法可以合并第2行和第4行,以便只能使用newtest.txt而不是在newtest2.txt中具有正确的输出。

最佳答案

使用括号:

((Get-Content c:\test.txt) -replace 'A','apple') -replace 'B','blue' | Out-File c:\newtest2.txt

如果您愿意,也可以这样做。没有理由不能两次使用-replace运算符。
$data = Get-Content c:\test.txt
$data -replace 'A','apple' -replace "B","Blue" | Out-File c:\newtest.txt

关于powershell - 如何在Powershell的一条语句中使用-replace两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117681/

相关文章:

powershell - Inno Setup - 以变量作为参数的编译后批处理文件 - 参数未转换为变量值

powershell - 无法从一个函数返回一个对象并将其传递给另一个

powershell - 使用powershell删除回收站中的旧文件

powershell - 替换两个特定字符串之间的行 - 在 cmd 中等效于 sed

powershell - 如果任何其他进程使用,如何编写 powershell 脚本来释放文件

azure - 使用/Powershell列出Azure中的所有存储帐户和容器

powershell - 2>&1在PowerShell中是什么意思

arrays - 如何将powershell命令的输出放入数组

powershell - 删除 VM 后从 Azure 资源管理器中删除 VHD

Powershell命令: rm -rf