How to trim blank spaces from PowerShell output?
Asked Answered
V

4

5

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"

Verlinevermeer answered 19/7, 2011 at 4:49 Comment(1)
This is what I used in the end: 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 $outputTxtFileVerlinevermeer
I
6

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}
Indogermanic answered 19/7, 2011 at 6:39 Comment(0)
O
6

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.

Ozone answered 19/7, 2011 at 6:50 Comment(0)
J
6

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.

Jigaboo answered 19/7, 2011 at 8:2 Comment(1)
The Export-Csv worked up to the point where the .csv file grew to ~100MB and PowerShell error'd out with a "OutOfMemoryException" exception. +1 for the link.Verlinevermeer
K
0

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}
Kwarteng answered 20/1, 2021 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.