Removing extra blank space from PowerShell output

…衆ロ難τιáo~ 提交于 2021-02-11 12:47:03

问题


I have a PowerShell program to replace some strings with another from an input file but the output file has an extra space/line when compared to input file.

$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
}

来源:https://stackoverflow.com/questions/52719227/removing-extra-blank-space-from-powershell-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!