【问题标题】:PowerShell Out-file -width does not truncate content when exporting to a file导出到文件时,PowerShell Out-file -width 不会截断内容
【发布时间】:2015-06-13 23:55:18
【问题描述】:

我有 Windows 7 和 PowerShell 4.0。将内容导出到文件时,-Width 参数不会根据给定设置进行格式化。这是我正在尝试做的一个示例:

"It is a nice hot sunny day" | Out-File -FilePath ".\output.txt" -Encoding ASCII -Width 10

导出的结果不会在第 10 个字符处被截断。它根本不会被截断。我不知道出了什么问题。

【问题讨论】:

    标签: powershell powershell-4.0


    【解决方案1】:

    这让我有些意外,但显然,-Width 参数仅适用于格式化对象:

    字符串作为输入,无效

    PS C:\> "It is a nice hot sunny day" |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
    It is a nice hot sunny day
    

    Format-Table,这行得通

    PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
    
    text
    -----
    It is a...
    

    Format-List,这可行,但方式很奇怪:

    PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
    
    text : It
           is
           a n
           ice
            ho
           t s
           unn
           y
           day
    

    所以,我们可以得到的最接近的可能是Format-Table -HideTableHeaders

    PS D:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table -HideTableHeaders|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
    
    It is a...
    

    受@Matt 回答的启发,您可以编写自己的函数来截断字符串:

    Function Resize-String {
        Param(
            [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
            [string[]]$InputObject,
            [int]$Width = 10
        )
    
        process{
            $InputObject.Substring(0,[System.Math]::Min([string]$InputObject[0].Length,$Width))
        }
    }
    
    PS C:\> "It is a nice hot sunny day" |Resize-String|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
    It is a ni
    

    【讨论】:

      【解决方案2】:

      我也对-Width 参数感到困惑,因为我预计您输入的内容会执行某事。但是,如果它确实有效,我认为您无论如何都不会得到您期望的结果。如果您在 TechNet 上阅读 Out-File,您会看到宽度...

      指定每行输出的字符数。任何额外的字符都会被截断

      所以我有一个类似的答案,我接受字符串输入并基于整数换行。

      Function Get-Folded{
          Param(
              [string[]]$Strings,
              [int]$Wrap = 50
          )
          $strings  -join " " -split "(.{$wrap,}?[ |$])" | Where-Object{$_}
      }
      

      所以如果我们试试你的文字

      Get-Folded "It is a nice hot sunny day" -Wrap 5
      

      将呈现此输出,其优点是不会破坏单词。

      It is 
      a nice 
      hot sunny 
      day
      

      这现在可以很容易地通过管道传输到文件。更多解释来自my answer to another question

      【讨论】:

        【解决方案3】:

        不幸的是,文档对此不是很清楚,但似乎-Width 仅适用于将某种对象呈现到文本输出并且字符串不计算在内的情况。

        例如:

        ([PSCustomObject]@{Value = "It is a nice hot sunny day"}) | Out-File -FilePath ".\output.txt" -Encoding ASCII -Width 10
        

        对于字符串,甚至数组,它根本不使用-Width

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-29
          • 2016-03-11
          • 2020-05-15
          • 1970-01-01
          相关资源
          最近更新 更多