我正在尝试使用 PowerShell 替换多个文本文件的某些列中的字符。我让它工作得很好,只是我需要忽略每个文件中的第一行和最后一行,我无法让它工作。
这是我到目前为止:
$Location = "C:\Users\gerhardl\Documents\Tenacity\TEMP\POWERSHELL TESTS"
$Data = "$Location\*.TXT"
$Output = "$Location\Fixed"
Get-Item $Data |
ForEach-Object {
$file = $_
$_ |
Get-Content |
ForEach-Object {
$Beginning = $_.Substring(0,105)
$Account = $_.Substring(105,20) -replace "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]", " "
$End = $_.Substring(125)
'{0}{1}{2}' -f $Beginning,$Account,$End
} |
Set-Content -Path (Join-Path $Output $file.Name)
}
Run Code Online (Sandbox Code Playgroud)
我知道有类似的线程,但似乎我的 For Each 循环不能很好地处理这些建议。
您可以使用-Skip 1和-SkipLast 1:
Get-Content $file | Select-Object -Skip 1 | Select-Object -SkipLast 1
Run Code Online (Sandbox Code Playgroud)
编辑 PS < 5 :
$text = Get-Content $file | Select-Object -Skip 1
$newText = $text.GetRange(0,($text.Count - 1))
$newText
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5635 次 |
| 最近记录: |