我正在使用PowerShell脚本查找正则表达式的所有出现并将其输出到文件。我有两个目标:
这会输出以下内容:
这是因为文件有前导空格(实际上是缩进/制表符,但输出为白色空格)。我不能更改原始文件并删除前导空格,因为它们是我们的生产文件/SQL脚本。
我想要修剪行列的前导空格,使输出看起来像这样:
- 从列值中删除前导空格
- 为额外字段(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脚本。
我想要修剪行列的前导空格,使输出看起来像这样:
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
我试图使用“Format-Table -AutoSize -Wrap”以及其所有变体,但出现了类似于“Format-Table:找不到与参数名width=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
- xcodr