Powershell - 奇怪的换行行为

时间:2014-07-25 11:15:54

标签: powershell

我是Powershell的新手,如果这是一个显而易见的问题,请道歉。我有以下函数来分隔字符串:

 [int[]]$rec02 = 3,2,1,4,3,3,5,5,6,1,19,45,2,3,50

 function delimitString([string]$text, [int[]]$arrDelims)
        {
                [string]$out;
                [int]$total=0;
                foreach($d in $arrDelims)
                {        
                    $out += $text.substring($total,$d)+",";
                    $total +=$d;
                }

                $out += $text.substring($total,$text.length-$total)+",";
                return $out;
        }

如果调用如下:     delimitString" gjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffllllllllll" $ rec02

如何抑制/为什么在输出的开头/结尾有新的换行符/回车符?

GJJ,JJ,J,JJJJ,JJJ,JJJ,JJJJJ,JJJJJ,jjjjjj,J,jjjjlllllllllllllll,llllllllllllllll lllllllllllllllllllllllllllll,LL,LLL,llllllllllllllllllllllllllffffffffffffffff FFFFFFFF,ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff fffffffffffffffffffffffffffffffffffffffffllllllllll,

我最终将其写入文件,因此我在输出中以空白行结束。

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解这个问题,但您似乎正在尝试以这种方式初始化变量[string]$out;。实际上,您只是将$out变量(之前从未使用过的)打印为字符串,因此您无法通过使用此代码$out +=来添加某些内容,从而获得预期的结果到一个字符串。

尝试将此[string]$out;更改为此$out = "";,您应该能够正确运行代码(现在$outString)。

答案 1 :(得分:1)

[string]$out不是初始化。它将输出变量命名为'out'作为返回的补充。由于没有$out变量,因此在输出之前它会使用默认值(空字符串)创建它。以这种方式输出时,它会在每个输出行的末尾添加换行符。

所以,[string]$out实际上是(在你的情况下)New-Object -TypeName 'System.String' -ArgumentList @([System.String]::Empty),因为你看到它是返回一个对象的函数

同样的问题是New-Item功能。它返回新项目的对象,因此,当您在函数中使用它时,您应该将输出发送到null

    Function TestReturn {
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File'
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File'
    return "Hello, World"
}
TestReturn

返回

;    Mode                LastWriteTime     Length Name                                                                                                                                             
;----                -------------     ------ ----                                                                                                                                             
;-a---        26.07.2014      9:08          0 tmpD9B4.tmp.test                                                                                                                                 
;-a---        26.07.2014      9:08          0 tmpDA03.tmp.test                                                                                                                                 
;Hello, World

要省略这一点,您应该使用New-Item ... | Out-Null或将值放入变量$temp = New-Item ... 所以正确的形式是(向右滚动!)

        Function TestReturn {
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File' | Out-Null
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File' | Out-Null
    return "Hello, World"
}
TestReturn

因此,您应该使用$out=''或(它是您的典型代表)$out=[String]''而不是[string]$out。那些线条做了非常不同的事情。你行试图创建和输出变量。第$out=''行只是初始化它。

要写入文件,您应使用$somevar | Out-FileOutFile -InputObject表格

[int[]]$rec02 = @(3,2,1,4,3,3,5,5,6,1,19,45,2,3,50)

 function delimitString([string]$text, [int[]]$arrDelims)
        {
                $out='';
                $total=0;
                foreach($d in $arrDelims)
                {        
                    $out += $text.substring($total,$d)+",";
                    $total +=$d;
                }

                $out += $text.substring($total,$text.length-$total)+",";
                return $out 
        }
$tempFile = [System.IO.Path]::GetTempFileName()
$somevar = delimitString "gjjj...." $rec02 
Write-Host -ForegroundColor Red -NoNewline "BEGIN"
Write-Host -ForegroundColor Green -NoNewline $somevar
Write-Host -ForegroundColor Red "END"

Write-Host -ForegroundColor Red "Out Length: $($somevar.Length)"
$tempFile = [System.IO.Path]::GetTempFileName()
Write-Host -ForegroundColor Green "TempFile: $($tempFile)"
Out-File -FilePath $tempFile -Encoding 'Ascii' -InputObject $somevar
$somevar = Get-Content -Path $tempFile -Encoding 'Ascii'

注意:带编码的输出文件'UTF-8'在开头添加了BOM记录。为避免这种情况,请使用[System.IO.File]::WriteAllText($path,$data,[System.Text.Encoding]::UTF8)