如何删除 PowerShell 输出中的空格?

发布于 2024-11-24 17:41:54 字数 1597 浏览 3 评论 0原文

我正在使用 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 列将占据行中大约一半的空间。我可以指定行号的宽度吗?我尝试过 -AutoSize 标志,但这似乎效果不佳。我已经尝试

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

过此方法的所有变体,但出现“格式表:找不到与参数名称宽度=50匹配的参数”之类的错误

I am using a PowerShell script to find all occurrences of a regex expression and output it to file. I have two objectives for this question.

  1. Remove leading white space from a column's value
  2. Specify a width for an extra field (LineNumbers)

This is what I have so far:

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

This outputs the following:

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. 

This is because the files have leading white spaces (actually indents/tab spaces, but outputted as white spaces). I can't change the original files and remove the leading white space as they are our production files/SQL scripts.

I want to trim the leading white space for the Line column so that the output looks like this:

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. 

And, if I add the LineNumbers column by using

-property LineNumbers 

then the LineNumbers column take up about half the space in the row. Can I specify the width of the LineNumbers? I've tried the -AutoSize flag, but this doesn't seem to work well. I've tried

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

and all variations of this, but I get errors to the likes of "Format-Table: A parameter cannot be found that matches parameter name width=50"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一紙繁鸢 2024-12-01 17:41:54

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

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

I can't test it right now, but I think this should do the trick or at least get you going in the right direction:

gci -recurse -include *.* | Select-String -pattern $regexPattern |`
Format-Table Path, @{Name='Line'; Expression={$_.Line -replace '^\s+', ''}; Width=50}
转身以后 2024-12-01 17:41:54

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

You can use the TrimStart() method to remove leading spaces. There's also TrimEnd() to remove characters from the end, or Trim() to remove characters from both sides of the string.

烟酉 2024-12-01 17:41:54

我不会使用 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 个字符的列,右对齐并添加一个
space 第二个项目的列,每列 20 个字符,右对齐
为第 3 个 8 个字符的项目添加一个空格 A 列,左对齐。

I would not use Format-Table for output to a file.

I'd rather use Export-Csv

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

If you still want to use Format-Table I reccomend reading this aricle
http://www.computerperformance.co.uk/powershell/powershell_-f_format.htm

Quote:

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

A column for the 1st item of 28 characters, right-aligned and adds a
space A column for the 2nd item of 20 characters right-aligned and
adds a space A column for the 3rd item of 8 characters left-aligned.

年华零落成诗 2024-12-01 17:41:54

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

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

In case someone from this decade comes looking here, there is an alternative using Trim():

gci -recurse -include *.* | Select-String -pattern $regexPattern |`
Format-Table Path, @{Name='Line'; Expression={$_.Line.Trim()}; Width=50}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文