Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to trim blank spaces from PowerShell output?
    text
    copied!<p>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. </p> <ol> <li>Remove leading white space from a column's value</li> <li>Specify a width for an extra field (LineNumbers)</li> </ol> <p>This is what I have so far:</p> <pre><code>gci -recurse -include *.* | Select-String -pattern $regexPattern |` Format-Table -GroupBy Name -Property Path, Line -AutoSize </code></pre> <p>This outputs the following: </p> <pre><code>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. </code></pre> <p>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.</p> <p>I want to trim the leading white space for the Line column so that the output looks like this:</p> <pre><code>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. </code></pre> <p>And, if I add the LineNumbers column by using</p> <pre><code>-property LineNumbers </code></pre> <p>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 </p> <pre><code>LineNumber;width=50 LineNumber width=50 LineNumber -width 50 </code></pre> <p>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"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload