Functions/Remove-BlankOrComment.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
filter Remove-BlankOrComment { <# .SYNOPSIS A simple text filter to remove blank lines or lines that begin with a comment character. .DESCRIPTION A simple text filter to remove blank lines or lines that begin with a comment character. By default the begin comment token is '#'. The comment character could be preceded by zero or more whitespace characters (space, tab) .PARAMETER String The input string array .PARAMETER BeginComment The token indicating the beginning of a single line comment. Defaults to '#' .EXAMPLE '#Hello','','There' | Remove-BlankOrComment Would return: There .EXAMPLE Remove-BlankOrComment -verbose Would return: VERBOSE: No input .EXAMPLE '#Hello','','There' | Remove-BlankOrComment -verbose VERBOSE: Line 1 is [#Hello] VERBOSE: Line 2 is [] VERBOSE: Line 3 is [There] There VERBOSE: Ending Remove-BlankOrComment .EXAMPLE get-content $env:appdata\code\user\settings.json | Remove-BlankOrComment -BeginComment '//' | convertfrom-json To create a psobject of your VsCode settings.json file .OUTPUTS [string[]] .NOTES Changed logic to regex escape on the begin comment token. .LINK about_Functions #> #region Parameter [CmdletBinding(ConfirmImpact = 'None')] [OutputType('string[]')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter','')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')] Param( [Parameter(Position = 0, ValueFromPipeline)] [string[]] $String, [string] $BeginComment = '#' ) #endregion Parameter begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" $Line = 0 $Count = 0 $BeginComment = [Regex]::Escape($BeginComment) } process { foreach ($s in $String) { $Line ++ $Count ++ Write-Verbose -Message "Line $Line is [$($s)]" if ($s) { $s | Where-Object { $_ -notmatch "^[\s]*$BeginComment" -and $_ -notmatch '^[\s]*$' } } } } end { if (-not $Count) { Write-Verbose -Message 'No input' } Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |