Share via


Remove blank (lines) from an array

Question

Monday, March 7, 2016 11:51 AM

I've tried everyting it just down't seem to work:

#Combine both result arrays
$result

$lines = (($result -replace '((udp|tcp) \d{3})', "`$1`n") -split "`n")

foreach($line in $lines){if ([string]::IsNullOrEmpty($line)) {$lines =-$line}}

Any ideas?  The array is $lines, it has already been split into lines from the origina $results array.

Alter De Ruine

All replies (14)

Monday, March 7, 2016 1:04 PM ✅Answered

No quite sure why it wasn't working.  Anyway I worked out that the blank lines are 1 char in length, who knows what that char is? :-)

$result

$linearray =@()

$lines = (($result -replace '((udp|tcp) \d{3})', "`$1`n") -split "`n")

$lines = $lines | select -last 10
foreach ($line in $lines){if ($line.length -gt 1){ $Linearray += $line }}
$linearray

Alter De Ruine


Monday, March 7, 2016 12:02 PM | 1 vote

What is in your results array? You could try something like this - 

$lines = $lines | ? {$_ -ne ""}

[string](0..21 | % {([char][int](22+
("5275778524449275827587
42505554247789249585").Substring(($_*2),2)))})`
-replace " "

LinkedIn:   

Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


Monday, March 7, 2016 12:08 PM

Hi,

Thanks, I tried it again but still no luck.  IT is an output from my firewall, pulled from a webpage

 705: 11:58:02.735740       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 706: 11:59:02.745948       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 707: 11:59:02.754050       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 708: 12:00:02.762076       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 709: 12:00:02.770178       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 710: 12:01:02.778569       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 711: 12:01:02.786778       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 712: 12:02:02.795124       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 713: 12:02:02.802936       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 714: 12:03:02.811100       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 715: 12:03:02.819476       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

Alter De Ruine


Monday, March 7, 2016 12:32 PM

Ok, how about?

$lines = $lines | ? {$_ -ne "^\s+"}

[string](0..21 | % {([char][int](22+
("5275778524449275827587
42505554247789249585").Substring(($_*2),2)))})`
-replace " "

LinkedIn:   

Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


Monday, March 7, 2016 12:49 PM

Braham's first suggestion should work without issue:

PS C:\Scripts\PowerShell Scripts\Misc Testing\3-07-2016> Get-Content .\inputFile.txt
 705: 11:58:02.735740       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 706: 11:59:02.745948       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 707: 11:59:02.754050       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 708: 12:00:02.762076       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 709: 12:00:02.770178       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 710: 12:01:02.778569       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 711: 12:01:02.786778       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 712: 12:02:02.795124       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 713: 12:02:02.802936       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

 714: 12:03:02.811100       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861

 715: 12:03:02.819476       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437

PS C:\Scripts\PowerShell Scripts\Misc Testing\3-07-2016> Get-Content .\inputFile.txt | Where { $_ -ne ''}
 705: 11:58:02.735740       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437
 706: 11:59:02.745948       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861
 707: 11:59:02.754050       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437
 708: 12:00:02.762076       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861
 709: 12:00:02.770178       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437
 710: 12:01:02.778569       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861
 711: 12:01:02.786778       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437
 712: 12:02:02.795124       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861
 713: 12:02:02.802936       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437
 714: 12:03:02.811100       4.5.6.7.5060 > 1.2.3.4.5060:  udp 861
 715: 12:03:02.819476       1.2.3.4.5060 > 4.5.6.7.5060:  udp 437


Monday, March 7, 2016 1:12 PM

You can have a look on the char-number by 

$a.ToCharArray() | foreach{[char][byte]$_}

if $a is your string. Then you can look in the unicode table.


Monday, March 7, 2016 2:07 PM

You could also just do a match on anything longer than 1 character

$arr = $arr | ?{$_ -match ".."}

[string](0..21 | % {([char][int](22+
("5275778524449275827587
42505554247789249585").Substring(($_*2),2)))})`
-replace " "

LinkedIn:   

Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


Monday, March 7, 2016 4:14 PM

gc .\spaces.txt | ? {$_}

Dan


Monday, March 7, 2016 7:29 PM

gc .\spaces.txt | ? {$_}

Dan

gc .\spaces.txt | ?{$_.trim()}


Monday, March 7, 2016 7:37 PM

gc .\spaces.txt | ? {$_}

Dan

gc .\spaces.txt | ?{$_.trim()}

I don't see the difference.

Dan


Monday, March 7, 2016 8:04 PM

No quite sure why it wasn't working.  Anyway I worked out that the blank lines are 1 char in length, who knows what that char is? :-)

$result

$linearray =@()

$lines = (($result -replace '((udp|tcp) \d{3})', "`$1`n") -split "`n")

$lines = $lines | select -last 10
foreach ($line in $lines){if ($line.length -gt 1){ $Linearray += $line }}
$linearray

Alter De Ruine

seriously?

Dan


Monday, March 7, 2016 9:39 PM

Mine will remove lines that contain all spaces as well as truly empty lines.
The original request was to remove blank lines, and in my mind a line composed of all spaces is a blank line.


Wednesday, March 9, 2016 10:39 PM

Mine will remove lines that contain all spaces as well as truly empty lines.
The original request was to remove blank lines, and in my mind a line composed of all spaces is a blank line.

Gotcha. I didn't think of trying it. I would assume the logfile would contain only empty lines plus lines with characters. Who knows?:-)

The "answer" to this question is the most hideous I've ever seen on these forums.

Dan


Thursday, March 10, 2016 7:37 AM

I could contribute another one that returns all lines containing an alphanumeric sign:

$lines = $lines | where{$_ -match "\w"}