Condividi tramite


PowerShell One-Liner: Stripping Comments and Blanks

I know, I know, XML is the new standard.  There are still times where flat files are easier.  Time is short, and I'm lazy.  (I'm also impatient and working on hurbis.  Geek-points if you can tell me where that's from.)

Anyhow,  here's a quick-and-dirty way to strip out comments, whitespace and blank lines from a text file.

(

Get-Content $listFile) -replace '#.*' -replace '^\s+' -replace '\s+$' | Where-Object {$_};

 Where-Object here has a very simple task.  If $_ is not null, then it's true, so pass it on.  PowerShell v1 has a nasty habit of iterating into nulls in an array, resulting in some fairly paranoid loop scripting.