【问题标题】:How to trim blank spaces from PowerShell output?如何从 PowerShell 输出中修剪空格?
【发布时间】:2011-10-08 05:11:29
【问题描述】:

我正在使用 PowerShell 脚本查找所有出现的正则表达式并将其输出到文件中。对于这个问题,我有两个目标。

  1. 从列值中删除前导空格
  2. 为额外字段(LineNumbers)指定宽度

这是我目前所拥有的:

gci -recurse -include *.* | Select-String -pattern $regexPattern |`
Format-Table -GroupBy Name -Property Path, Line -AutoSize

这会输出以下内容:

Path                      Line                                                   
----                      ----
C:\myRandomFile.txt       This is line 1 and it is random text.
C:\myRandomFile.txt                This is line 2 and it has leading white space.
C:\myNextRandomFile.txt            This is line 3 and it has leading white space.
C:\myLastRandomFile.txt                         This is line 4. 

这是因为文件有前导空格(实际上是缩进/制表符空格,但输出为空格)。我无法更改原始文件并删除前导空格,因为它们是我们的生产文件/SQL 脚本。

我想修剪 Line 列的前导空白,以便输出如下所示:

Path                      Line                                                   
----                      ----
C:\myRandomFile.txt       This is line 1 and it is random text.
C:\myRandomFile.txt       This is line 2 and it has no leading white space.
C:\myNextRandomFile.txt   This is line 3 and it has no leading white space.
C:\myLastRandomFile.txt   This is line 4 and this is how it should look. 

而且,如果我使用添加 LineNumbers 列

-property LineNumbers 

那么 LineNumbers 列占据了行中大约一半的空间。我可以指定 LineNumbers 的宽度吗?我已经尝试过 -AutoSize 标志,但这似乎效果不佳。我试过了

LineNumber;width=50
LineNumber width=50
LineNumber -width 50

以及所有这些变体,但我收到“格式表:找不到与参数名称宽度=50 匹配的参数”之类的错误

【问题讨论】:

  • 这是我最后用的:gci -recurse -include *.* | Select-String -pattern $regexPattern | Format-Table @{Name='Path'; Expression={$_.Path}; Width=80}, @{Name='Line'; Expression={$_.Line -replace '^\s+', ''}; Width=200} | Out-File $outputTxtFile

标签: powershell whitespace removing-whitespace


【解决方案1】:

我现在无法测试它,但我认为这应该可以解决问题,或者至少让你朝着正确的方向前进:

gci -recurse -include *.* | Select-String -pattern $regexPattern |`
Format-Table Path, @{Name='Line'; Expression={$_.Line -replace '^\s+', ''}; Width=50}

【讨论】:

    【解决方案2】:

    您可以使用 TrimStart() 方法删除前导空格。还有 TrimEnd() 用于从末尾删除字符,或 Trim() 用于从字符串两侧删除字符。

    【讨论】:

      【解决方案3】:

      我不会使用 Format-Table 输出到文件。

      我宁愿使用 Export-Csv

      gci -recurse -include *.* | Select-String -pattern $regexPattern |`
      select-object linenumber, path, line | Export-Csv c:\mycsv.csv -Delimiter "`t"
      

      如果你仍然想使用 Format-Table 我推荐阅读这篇文章 http://www.computerperformance.co.uk/powershell/powershell_-f_format.htm

      引用:

      "{0,28} {1, 20} {2,-8}" -f ` 创建:

      第一项的 28 个字符的列,右对齐并添加一个 空格 20 个字符的第 2 项的列右对齐和 为左对齐的 8 个字符的第 3 项添加一个空格 A 列。

      【讨论】:

      • Export-Csv 工作到 .csv 文件增长到 ~100MB 并且 PowerShell 出错并出现“OutOfMemoryException”异常。为链接 +1。
      【解决方案4】:

      如果这十年的人来找这里,还有一个使用 Trim() 的替代方法:

      gci -recurse -include *.* | Select-String -pattern $regexPattern |`
      Format-Table Path, @{Name='Line'; Expression={$_.Line.Trim()}; Width=50}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 2012-10-10
        • 2010-09-16
        • 2010-10-20
        相关资源
        最近更新 更多