Share via


How can I get a Select-Object Expression to trim empty spaces?

Question

Monday, January 9, 2012 8:09 PM

I am trying to output a GCI search result set to a file but when I open the file, I have a lot of dead space at the end of the line.  I am using this approach:

PS > dir 'C:\somefolder' | Where {($_.Extension -ne ".log") -and (-not ($_.PSIsContainer))}  | select @{Expression={($_.name + ".txt").ToString().Trim()};Label="Name"} | Out-File -FilePath 'C:\somefolder\logs\log.txt'

All replies (8)

Monday, January 9, 2012 8:16 PM ✅Answered | 1 vote

It needs a sub-expression to evalute properly:

dir 'C:\somedir' |
 Where {($_.Extension -ne ".log") -and (-not ($_.PSIsContainer))}  |
  select @{Expression={$(($_.name + ".txt").ToString().Trim())};Label="Name"} 

 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Monday, January 9, 2012 8:47 PM ✅Answered

I do not see a difference in both cases.

 

dir 'C:\Windows' |
 Where {($_.Extension -ne ".log") -and (-not ($_.PSIsContainer))}  |
  select @{Expression={$(($_.name + ".txt").ToString().Trim())};Label="Name"} | Out-String | Measure-Object -Character

Lines                         Words                                            Characters Property
-----                         -----                                            ---------- --------
                                                                                     7024

PS  >  dir 'C:\Windows' | Where {($_.Extension -ne ".log") -and (-not ($_.PSIsContainer))}  |
>> select @{Expression={($_.name + ".txt").ToString().Trim()};Label="Name"} | Out-String | Measure-Object -Character
>>

Lines                         Words                                            Characters Property
-----                         -----                                            ---------- --------
                                                                                     7024


PS >  dir 'C:\Windows' | Where {($_.Extension -ne ".log") -and (-not ($_.PSIsContainer))}| %{$_.name.trim() + ".txt"}
 |
>> Out-String | Measure-Object -Character
>>

Lines                         Words                                            Characters Property
-----                         -----                                            ---------- --------
                                                                                      994

Monday, January 9, 2012 8:25 PM

Why not using foreach instead of the select?

%{$_.name.trim() + ".txt"}


Monday, January 9, 2012 8:37 PM

So close. Those darn subexpressions get me every time.


Monday, January 9, 2012 8:38 PM

I am just in the habit of using select. No specific reason.


Monday, January 9, 2012 8:51 PM

Gotcha.  That's pretty straightforward.  Thanks for the clarification.  Can I extract your brain into a module?

Import-Module Kazun
Fix-Everyting *

Monday, January 9, 2012 8:59 PM

No it doesn't. It works fine as is... Will, did you try this and it changed the result?

The question is why is there trailing whitespace on each line in the file. That is always the case when sending table-formatted output to out-file, it will include all whitespace used for console formatting, including trailing whitespace. Sending List-formatted data or raw strings does not have the issue.

You can run below after dumping to the file, to delete any trailing whitespace:

$truncated = gc .\log.txt |%{ $_ -replace '\s+$', '' }
$truncated | out-file .\log.txt

Thanks,
-Lincoln


Tuesday, January 10, 2012 12:48 AM

 dir C:\somefolder |

   ? {($_.Extension -ne '.log') -and ($_.PSIsContainer -eq $False)} |

     % {$($_.Name) + '.txt'} >C:\somefolder\logs\log.txt

 

will not capture the trailing whitespace.

 

It also will not generate the header lines.

 

If needed to output file could be initiated with the header as static text.