'Print only non-blank lines in PowerShell

I have a text file with multiple lines. Many are blank, or so I am assuming from looking at the file contents. I would like to write/print only the lines that contain text. I am having trouble. Here is my code:

$test = Get-Content -Path '.\dummy-file.html'

# convert html file to text, save only the relevant info (no tags)
foreach ($line in $test) {
    $newline = $line -split ("<.*?>") -split ("{.*?}") # remove html and css tags
    $newline -replace "`n","" # thought this would get rid of blank lines. it doesn't
    $newline >> "test-ouput.txt" # save to new file
}

# read text file, print only lines with text
$test.ForEach({$_ -notmatch "`n"})

The above doesn't work, prints only booleans to the console, and even then, their values are wrong. The correct output, given the first 10 lines of $test, should be only two lines of text, eight of them are blank. However, the blank lines are printed.

I am new to regex, assuming it has something to do with that. My understanding of PowerShell is also novice. Thanks.



Solution 1:[1]

An easy solution for this without using regex would be using String.IsNullOrWhiteSpace(String) Method:

Get-Content -Path '.\dummy-file.html' | Where-Object {
    -not [string]::IsNullOrWhiteSpace($_)
}

It can be read as, all lines where this line is not an empty string or white spaces.

If you want to test using regex, you can use the -match operator. \S in regex matches any non-whitespace character.

Get-Content -Path '.\dummy-file.html' | Where-Object {
    $_ -match '\S'
}

An example below:

PS /> @'
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
         
a, luctus sit amet augue. Aliquam finibus,
          
felis luctus tincidunt dapibus, justo tellus finibus risus, et
          
in pharetra risus. Lorem ipsum dolor
'@ -split '\r?\n' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
a, luctus sit amet augue. Aliquam finibus,
felis luctus tincidunt dapibus, justo tellus finibus risus, et
in pharetra risus. Lorem ipsum dolor

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1