从PowerShell输出中删除多余的空格

时间:2018-10-09 10:49:19

标签: powershell

我有一个PowerShell程序,可以用输入文件中的另一个字符串替换某些字符串,但是与输入文件相比,输出文件具有额外的空格/行。

$InputACKFile = 'C:\Testing_Newline\Aj*.dat'

Write-Host "Found " ($InputACKFile | Measure-Object).Count " files to process"

$InputACKFile = Get-ChildItem $InputACKFile

foreach ($xfile in $InputACKFile) {
    Write-Host "Processing file: $xFile`r"

    #((Get-Content $xfile).Replace("~", "`n")) | Out-File $xfile.FullName
    [System.IO.File]::ReadAllText($xfile).Replace("~","`n") |
        Set-Content $xfile

    $flag = 0
    (Get-Content $xfile | ForEach {
        if ($_ -match "^PQ\*") {
            $_ -replace "test1", "test2"
        } elseif ($_ -match "^TA\*") {
            $_ -replace "test1", "test2"
        } elseif ($_ -match "^ABC\*RS\*") {
            $_ = '';
            $flag = 1
        } elseif ($_ -match "^(DE\*)([0-9]+)(\*[0-9]+)$" -and $flag -eq 1) {
            $_ -replace ($matches[2]), ([int]::Parse($matches[2])-1);
            $flag = 0
        } else{
            $_
        }
    }) | Out-File $xfile.FullName -Encoding Ascii

    ((Get-Content $xfile) -join("~")) | Set-Content $xfile.FullName
}

0 个答案:

没有答案