【发布时间】:2020-06-11 03:56:35
【问题描述】:
我有一个文件
AB*00*Name1First*Name1Last*test
BC*JCB*P1*Church St*Texas
CD*02*83*XY*Fax*LM*KY
EF*12*Code1*TX*1234*RJ
我需要在文件夹中的每个文件中将 CD 段中的第 5 个元素从 LM 替换为 ET。元素分隔符是 * 如上面示例文件内容中所述。我是 PowerShell 的新手,并尝试了如下代码,但不幸的是它没有给出预期的结果。有没有人可以提供一些帮助?
foreach($xfile in $inputfolder)
{
If ($_ match "^CD\*")
{
[System.IO.File]::ReadAllText($xfile).replace(($_.split("*")[5],"ET") | Set-Content $xfile
}
[System.IO.File]::WriteAllText($xfile),((Get-Content $xfile -join("~")))
}
【问题讨论】:
标签: powershell replace substring powershell-3.0 powershell-4.0