我有一个简单的文本文件,需要编写一个PowerShell脚本来替换文件内容中的某些部分。
我的当前脚本如下:
$content = Get-Content -path "Input.json"
$content -Replace '"(\d+),(\d{1,})"', '$1.$2' | Out-File "output.json"
是否可能不使用content变量将其写在一行中,就像这样?
Get-Content -path "Input.json" | ??? -Replace '"(\d+),(\d{1,})"', '$1.$2' | Out-File "output.json"
在第二个命令中,我如何在不使用$content变量的情况下使用第一个get-content命令的输出?是否存在自动powershell变量?
在管道中是否可以进行多个替换操作?
Get-Content -path "Input.json" | ??? -Replace '"(\d+),(\d{1,})"', '$1.$2' | ??? -Replace 'second regex', 'second replacement' | Out-File "output.json"
"\
n", "b" -replace('\n', 'a')将返回 'a', 'b'。
"`n", "b" -replace('\nb', 'a')仍将返回 "\
n", "b"。 - Nate AndersonGet-Content -Raw
一次性处理所有行,这样你就会得到一个单一的字符串返回。 - Joey$tmp = (Get-Content Input.json) -replace '"(\d+),(\d{1,})"', '$1.$2'; [System.IO.File]::WriteAllLines('output.json', $tmp)
(也许你需要在输出文件名上提供完整路径)。 - RorySet-Content -Path 'output.json'
也可以输出没有字节顺序标记的文本。 - Erik Barke