如何删除 PowerShell 输出中的空格?
我正在使用 PowerShell 脚本查找所有出现的正则表达式并将其输出到文件。我问这个问题有两个目的。
- 从列的值中删除前导空格
- 指定额外字段的宽度 (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.
- Remove leading white space from a column's value
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我现在无法测试它,但我认为这应该可以解决问题,或者至少让您朝着正确的方向前进:
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:
您可以使用 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.
我不会使用 Format-Table 输出到文件。
我宁愿使用 Export-Csv
如果您仍然想使用 Format-Table 我建议阅读这篇文章
http://www.computerperformance.co.uk/powershell/powershell_-f_format.htm引用
:
I would not use Format-Table for output to a file.
I'd rather use Export-Csv
If you still want to use Format-Table I reccomend reading this aricle
http://www.computerperformance.co.uk/powershell/powershell_-f_format.htm
Quote:
如果这个十年的人来看这里,还有一个使用 Trim() 的替代方案:
In case someone from this decade comes looking here, there is an alternative using Trim():